Python Comments
Comments are lines in your code that are ignored during execution. They help you and others
understand what the code does or why something is written a certain way.
- Single-Line Comments : Use the # symbol at the beginning of the line.
# This is a single-line comment
print("Hello, Python") # This prints a message
- Multi-Line Comments (Using #) : You can use # at the beginning of multiple lines to create multi-line comments.
# This is a comment
# spread over multiple
# lines using the hash symbol
- Multi-Line Comments (Using Triple Quotes) : Although not officially for comments, Python
allows multi-line strings using triple quotes (''' or """).
These are often used as docstrings
but can serve as block comments too.
'''
This is a multi-line comment
written using triple single quotes.
It is not executed by Python.
'''
print("Python is fun!")
- Docstrings : Special multi-line strings used to describe functions, classes, or modules.
def greet():
"""This function prints a greeting message."""
print("Hello!")
Why Use Comments?
- To explain complex logic.
- To remind yourself or help others understand code.
- To disable code temporarily for testing.
- Can be used to explain Python code.
- Can be used to make the code more readable.
Python Variables
A variable in Python is used to store data or information that can be referenced and
manipulated later in the program.
In simple terms, Variables are containers for storing data values.
Creating Variables
You don’t need to declare the type — just assign a value directly.
x = 5
name = "Mango Tree"
price = 99.99
Python automatically understands the data type (integer, string, float, etc.) based on the value.
Variable Naming Rules
Here are the rules for naming variables in Python:
- Start: Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
They cannot start with a digit (0-9).
- Characters: Variable names can contain letters, numbers (0-9), and underscores (_).
However, they cannot contain spaces or special characters (like @, #, $, etc.).
- Case Sensitivity: Variable names are case-sensitive.
For example: myVar, myvar,
and MYVAR are three different variables.
- No Spaces: Variable names cannot contain spaces. Use underscores to separate
words (e.g., my_variable).
- Reserved Words: Avoid using Python keywords (e.g., if, else, for, while, class, def, import, etc.)
as variable names.
- Length: Variable names can be of any length, but it's a good practice to keep them concise and meaningful.
- Convention: Use lowercase letters and underscores for variable names (e.g., my_variable) to improve readability.
Valid Example:
my_variable = 10
_my_var = "Hello"
variable2 = 3.14
userName = "John Doe"
Invalid Examples:
2my_variable = 10 #cannot start with a number
my-variable = 10 #cannot contain a hyphen
my variable = 10 #cannot contain spaces
- Reassigning Variables: In Python, you can reassign a variable to a new value or
even change its data type at any time.
To reassign a variable, simply use the assignment operator
(=) with the variable name on the left and the new value on the right.
Example:
x = 10 # x is assigned the integer value 10
print(x) # Output: 10
x = "hello" # x is reassigned to a string value
print(x) # Output: hello
x = True # x is reassigned to a boolean value
print(x) # Output: True
-
Multiple Assignments: Python allows you to assign values to multiple variables in a single line.
Example:
x, y, z = 1, 2, 3
print(x) # Output: 1
print(y) # Output: 2
print(z) # Output: 3
- Printing Variables: Use the print() function to display variable values.
Example:
x = 10
print(x) # Output: 10
- Printing Variables: Use the print() function to display variable values.
Example:
x = 10
print(x) # Output: 10
- To print a string, enclose it in single or double quotes.
Example:
- You can also use f-strings for formatted output:
Example:
name = "Ram"
age = 30
print(f"My name is {name} and I am {age} years old.")
- Dynamic Typing: Python is dynamically typed, meaning you don't have to declare the variable type explicitly.
Example:
value = 100 # int
value = "Text" # now string
- Variable Type Checking: You can check the data type using the type() function.
Example:
- Deleting a Variable: You can delete a variable using the del keyword.
Example:
Why Use Variables?
- Readability: Variables make code more readable by giving meaningful names to data.
- Reusability: Variables allow you to reuse values without repeating code.
- Flexibility: Variables can change values, making your code adaptable.