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.


# This is a single-line comment
print("Hello, Python")  # This prints a message

# This is a comment
# spread over multiple
# lines using the hash symbol

'''
This is a multi-line comment
written using triple single quotes.
It is not executed by Python.
'''
print("Python is fun!")

def greet():
    """This function prints a greeting message."""
    print("Hello!")


Why Use Comments?



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:



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


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


Example:

   x, y, z = 1, 2, 3
   print(x)  # Output: 1
   print(y)  # Output: 2
   print(z)  # Output: 3


Example:

   x = 10
    print(x)  # Output: 10


Example:

    x = 10
    print(x)  # Output: 10


Example:

    print("Hello, World!")


Example:

name = "Ram"
age = 30
print(f"My name is {name} and I am {age} years old.")


Example:

value = 100     # int
value = "Text"  # now string


Example:

x = 10
print(type(x)) 


Example:

x = 100
del x


Why Use Variables?