Python Notes
Python Notes
1. Variables in Python
A variable is a name that refers to a value stored in memory. Variables are used to store data that
your program can manipulate.
In Python, variables are dynamically typed, which means you don’t need to declare the
type of a variable explicitly.
Python automatically determines the type of the variable based on the value assigned to
it.
x = 10 Integer
i = 3.14 float
x= 2.8 double
with example
a=10
b=20
c=a+b
print(c)
Loops allow you to execute a block of code multiple times. Python provides two main types of
loops: for and while.
🔁 1. for Loop
The for loop is used to iterate over a sequence like a list, string, or range.
for i in range(5): # 0 to 4
print(i)
2. while Loop
Example 1: Count to 5
count = 1
while count <= 5:
print(count)
count += 1
Calling a Function:
You call a function by writing its name followed by parentheses and passing values (called
arguments) inside the parentheses.
A function is a block of code that performs a task. You define it once, and then you can call it
whenever you want.
Step-by-step:
Step 1: Define the function
def say_hello(name):
print("Hello", name)
Here:
result = a + b
add_numbers(3, 5)
result = a + b
You use the key inside square brackets [] to get the value.
student["age"] = 21
student["subject"] = "Math"
print(student)
Output:
python
{'name': 'Alice', 'age': 21, 'grade': 'A', 'subject': 'Math'}
You cannot have two identical keys in a dictionary. The last one will overwrite the previous
one.
python
data = {
"id": 1,
"id": 2
}
print(data["id"])