Python 2.10 : Rescue buoy : print, input, str, int, try
Cette vidéo récapitule les notions de base Python vues jusqu'ici. C'est une "bouée de secours" pour vérifier que tout est bien assimilé avant de poursuivre. Plusieurs points méritent une vigilance particulière car ils sont la source d'erreurs récurrentes chez les débutants.
Les pièges courants
Premier point : Python est sensible à la casse, exactement comme Java. Une variable nom et une variable Nom sont deux variables différentes. Si vous déclarez nom = input("Quel est ton nom ?") puis essayez d'afficher print(Nom) avec une majuscule, vous obtenez une erreur "NameError: Nom is not defined". Toujours respecter la casse exacte.
Deuxième point : les noms de variables ne contiennent jamais d'espaces. nom de la personne n'est pas valide. On utilise des underscores : nom_de_la_personne. C'est la convention snake_case en Python.
Troisième point : la déclaration vient toujours AVANT l'utilisation. Si vous appelez print(nom) avant d'avoir affecté nom = "Alice", Python lève une erreur. L'ordre du code compte. Quatrième point : l'indentation est syntaxique en Python. Contrairement à Java ou JavaScript qui utilisent des accolades, Python délimite les blocs par l'indentation :
if age >= 18:
print("Majeur")
print("Peut voter")
print("Toujours affiché")
Cinquième point : ne jamais oublier les deux-points à la fin d'une définition de fonction, condition ou boucle. def ma_fonction() sans deux-points provoque une SyntaxError. Sixième point : l'opérateur + se comporte différemment selon les types :
- Entre deux chaînes : concaténation,
"Hello " + "World"donne "Hello World" - Entre deux entiers : addition,
2 + 3donne 5 - Entre une chaîne et un entier : erreur TypeError
- Solution : convertir explicitement avec
str(nombre)ouint(chaine) - La virgule dans
print("Vous avez", age, "ans")permet de passer plusieurs arguments séparés sans souci de type
Avec ces six points en tête, vous éviterez 80% des erreurs des débutants. La prochaine vidéo abordera les boucles while.
Summary
This lesson provides a rescue buoy (recap) of fundamental Python concepts including variable declaration and initialization, proper naming conventions, and the critical importance of correct syntax. It covers the print() function, type conversion with str() and int(), string concatenation, and demonstrates how Python's indentation and colons are mandatory structural requirements that prevent common errors.
Key points
- Variables must be declared and initialized before use; the order matters (declaration → initialization → function call)
- Variable names are case-sensitive, cannot contain spaces, and must follow naming conventions
- Python requires colons (:) after function definitions, conditionals, and loops, and enforces indentation for code blocks
- The print() function can accept multiple arguments separated by commas; strings can be concatenated using the + operator
- Type conversion is necessary: use str() to convert integers to strings, and int() to convert strings to numbers for arithmetic operations
- NameError occurs when variables are referenced before they are declared or when naming conventions are violated
FAQ
Why does Python require colons and indentation in code blocks?
Colons (:) mark the start of a new code block (functions, conditionals, loops), and indentation defines the block's scope. Python uses indentation instead of curly braces for syntax structure, making code readability mandatory—missing indentation or colons causes IndentationError or SyntaxError.
What is the difference between + and comma (,) when using print()?
Using + concatenates strings into a single string before printing. Using commas separates multiple arguments, and print() displays them with a space between each argument. Additionally, + cannot concatenate strings and integers directly; you must convert integers to strings with str() first.
What is a NameError and how do I fix it?
A NameError occurs when you reference a variable that has not been declared or initialized, or when the variable name is misspelled (Python is case-sensitive). Always declare and initialize variables before using them, and ensure variable names match exactly when referencing them.