1. Executing Python Syntax:
- Python code is executed line by line from top to bottom.
- To execute Python code, you can save it in a
.py
file and run it using thepython filename.py
command in the terminal/command prompt. - Alternatively, you can use the Python interactive shell by typing
python
in the terminal/command prompt, followed by directly typing and executing code line by line.
2. Python Indentation:
- Python uses indentation (whitespace at the beginning of lines) to define code blocks instead of braces or keywords.
- Use consistent indentation (typically 4 spaces) to maintain readability and to define blocks of code under control structures, functions, classes, etc.
- Example:python
if x > 5: print("x is greater than 5") print("Still inside the if block") print("Outside the if block")
3. Python Variables:
- Variables are used to store data in Python.
- In Python, you don't need to declare a variable explicitly. You can directly assign a value to a variable.
- Example:python
message = "Hello, Python!" count = 5 pi = 3.14
4. Python Comments:
- Comments are used to add explanatory notes to your code.
- In Python, you can add single-line comments using the
#
symbol. - Example:python
# This is a single-line comment print("Hello, Python!") # This is another comment
- You can also add multi-line comments using triple quotes (
'''
or"""
) at the beginning and end of the comment block. - Example:python
''' This is a multi-line comment. It can span multiple lines. ''' print("Hello, Python!")
Remember to follow proper syntax and indentation rules while writing Python code. Indentation errors can cause the code to produce unexpected results or even result in syntax errors. Regularly practice writing code and experiment with different Python constructs to enhance your understanding.
<< Previous Next >>