« Python : Les variables » : différence entre les versions

De www.yakakliker.org
Aucun résumé des modifications
Aucun résumé des modifications
 
(2 versions intermédiaires par le même utilisateur non affichées)
Ligne 13 : Ligne 13 :
=== Types de variables ===
=== Types de variables ===
[[Fichier:Capture d’écran du 2024-03-22 15-29-41.png|sans_cadre]]
[[Fichier:Capture d’écran du 2024-03-22 15-29-41.png|sans_cadre]]
=== Variables stockées dans un fichier externe (ex : .env) ===
<syntaxhighlight lang="bash">
db_user = os.getenv('POSTGRES_USER')
db_password = os.getenv('POSTGRES_PASSWORD')
db_host = os.getenv('POSTGRES_HOST')
db_port = os.getenv('POSTGRES_PORT')
db_name = os.getenv('POSTGRES_DB')
db_schema = os.getenv('POSTGRES_SCHEMA')
</syntaxhighlight>
* Dans le fichier :
<syntaxhighlight lang="bash">
POSTGRES_HOST=XXXXX
POSTGRES_PORT=XXXXX
POSTGRES_DB=XXXXX
POSTGRES_USER=XXXXX
POSTGRES_SCHEMA=XXXXX
POSTGRES_PASSWORD=XXXXX
</syntaxhighlight>
== Les types de variable ==
{| class="wikitable"
|'''Type'''
|'''Nom Python'''
|'''Exemple'''
|'''Description'''
|-
|'''Entier'''
|<code>int</code>
|<code>age = 25</code>
|Nombres sans virgule.
|-
|'''Flottant'''
|<code>float</code>
|<code>prix = 19.99</code>
|Nombres avec virgule (utilisez un point <code>.</code>).
|-
|'''Chaîne'''
|<code>str</code>
|<code>nom = "Alice"</code>
|Texte entouré de guillemets.
|-
|'''Booléen'''
|<code>bool</code>
|<code>est_pret = True</code>
|Vrai ou Faux (<code>True</code> ou <code>False</code>).
|}
=== Exemple de script simple avec des variables ===
<syntaxhighlight lang="python3">
# Définition des variables
utilisateur = "Apprenant"
niveau = 1
score = 85.5
# Affichage avec une "f-string" (méthode moderne pour insérer des variables)
print(f"Utilisateur : {utilisateur}")
print(f"Niveau actuel : {niveau}")
print(f"Progression : {score}%")
# On peut modifier une variable
niveau = niveau + 1
print(f"Félicitations, vous passez au niveau {niveau} !")
</syntaxhighlight>


=== Liens ===
=== Liens ===
https://dyma.fr/developer/list/chapters/core/619556fdd5fe7e09d4aba395/lesson/python/61969e5866ef0d5e68f7f66d/2/2
https://dyma.fr/developer/list/chapters/core/619556fdd5fe7e09d4aba395/lesson/python/61969e5866ef0d5e68f7f66d/2/2
[[Catégorie:Python]]
[[Catégorie:Python]]
[[Catégorie:Scripts]]
[[Catégorie:Scripts]]
‎<html>
<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script>
<script>
  kofiWidgetOverlay.draw('yakakliker', {
    'type': 'floating-chat',
    'floating-chat.donateButton.text': 'Café',
    'floating-chat.donateButton.background-color': '#00b9fe',
    'floating-chat.donateButton.text-color': '#fff'
  });
</script>
‎</html>




‎<html>
‎<html>
<a href="https://www.compteurdevisite.com" title="compteur web gratuit sans pub"><img src="https://counter6.optistats.ovh/private/compteurdevisite.php?c=b4epghealnwlf7wuq7gn3ygll9aywrfx" border="0" title="compteur web gratuit sans pub" alt="compteur web gratuit sans pub"></a>
<a href="https://www.compteurdevisite.com" title="compteur web gratuit sans pub"><img src="https://counter6.optistats.ovh/private/compteurdevisite.php?c=b4epghealnwlf7wuq7gn3ygll9aywrfx" border="0" title="compteur web gratuit sans pub" alt="compteur web gratuit sans pub"></a>
‎</html>
‎</html>

Dernière version du 20 janvier 2026 à 14:39

Déclarer une variable

nom_de_la_variable = uneValeur

Afficher une variable

variable = 'Bonjour tout le monde'

print(variable)

Types de variables

Variables stockées dans un fichier externe (ex : .env)

db_user = os.getenv('POSTGRES_USER')
db_password = os.getenv('POSTGRES_PASSWORD')
db_host = os.getenv('POSTGRES_HOST')
db_port = os.getenv('POSTGRES_PORT')
db_name = os.getenv('POSTGRES_DB')
db_schema = os.getenv('POSTGRES_SCHEMA')
  • Dans le fichier :
POSTGRES_HOST=XXXXX
POSTGRES_PORT=XXXXX
POSTGRES_DB=XXXXX
POSTGRES_USER=XXXXX
POSTGRES_SCHEMA=XXXXX
POSTGRES_PASSWORD=XXXXX

Les types de variable

Type Nom Python Exemple Description
Entier int age = 25 Nombres sans virgule.
Flottant float prix = 19.99 Nombres avec virgule (utilisez un point .).
Chaîne str nom = "Alice" Texte entouré de guillemets.
Booléen bool est_pret = True Vrai ou Faux (True ou False).

Exemple de script simple avec des variables

# Définition des variables
utilisateur = "Apprenant"
niveau = 1
score = 85.5

# Affichage avec une "f-string" (méthode moderne pour insérer des variables)
print(f"Utilisateur : {utilisateur}")
print(f"Niveau actuel : {niveau}")
print(f"Progression : {score}%")

# On peut modifier une variable
niveau = niveau + 1
print(f"Félicitations, vous passez au niveau {niveau} !")

Liens

https://dyma.fr/developer/list/chapters/core/619556fdd5fe7e09d4aba395/lesson/python/61969e5866ef0d5e68f7f66d/2/2


compteur web gratuit sans pub