An introduction to Python's basic syntax and variables.
Basic Syntax
Python syntax is relatively straightforward and uses indentation to define code blocks. Here's a simple example of a Python script that prints “Hello, World!”:
print(“Hello, World!”)
Variables
Variables are used to store data in Python. You can think of variables as containers that hold values. Unlike some other programming languages, Python is dynamically typed, meaning you don't need to declare the data type of a variable before assigning a value to it.
Variable Naming Rules
- Variable names can contain letters, numbers, and underscores. - Variable names must start with a letter or an underscore. - Variable names are case-sensitive. - Variable names cannot be reserved keywords like `print`, `if`, `else`, etc.
Examples of Variables
String Variables
name = “Alice”
Integer Variables
age = 25
Float Variables
height = 5.9
Boolean Variables
is_student = True
Multiple Assignments
You can also assign multiple variables in a single line:
a, b, c = 1, 2, 3
Comments
Comments are used to explain your code and are ignored by the Python interpreter. You can use comments to make your code more readable.
# This is a comment
Conclusion
Python's simple syntax and powerful features make it an excellent choice for beginners and experienced programmers alike. In this introduction, we covered basic syntax, variable declaration, and comments. As you continue learning Python, you'll explore more advanced topics and learn how to use Python to build various applications.