By the end of this lesson, you will be able to:
:information_source: Variable is a container for storing data values. Think of it like a labeled box where you can put information and use it later in your program.
Variables let you store and change data in your programs.
Good programmers choose clear names that explain what the variable holds.
Python makes it easy - you create a variable by giving it a value. No special commands needed!
score1
, player2
age
is better than AGE
total_score
, player_name
1player
, 99bottles
player name
, total score
Variables can hold different kinds of information. Each type has a special purpose:
:bulb: Python automatically knows what type of data you're storing - you don't need to tell it!
Built-in Python Data Types
Example Data Type Description "Hello World" string (str) Text and words 999 integer (int) Whole numbers 3.145 float Decimal numbers False boolean (bool) True or False values ["tiger", "cat", "dog"] list Ordered collection you can change ("tiger", "cat", "dog") tuple Ordered collection you can't change `{"animal": "tiger", "type": "carnivore"}` dictionary (dict) Key-value pairs :emoji: Understanding Operators
:information_source: Operators are special symbols that tell Python to do math or other operations. The numbers or values they work with are called operands.
Mathematical Operators in Python
Operator Operation Example Result (if x=10, y=3) + Addition x + y 13 - Subtraction x - y 7 * Multiplication x * y 30 / Division x / y 3.333... ** Power x ** y 1000 % Remainder x % y 1 :emoji: Order of Operations (PEMDAS)
When Python sees multiple operators, it follows the PEMDAS rule:
:memo: Note Parentheses -> Exponents -> Multiplication/Division -> Addition/Subtraction
Just like in math class!
:rocket: What We'll Learn Through Examples
- Assign values to variables
- Check data types of variables
- Calculate using mathematical operators
- Combine data and convert between types
:bulb: Basic Variable Assignment
python
name = "My Name is Chong Wei" age = 15 print(name) print (age)
Expected output:
csharp
My Name is Chong Wei 15
python
print(type(name))
Expected output:
arduino
\<class 'str'\>
python
print(type(age))
Expected output:
arduino
\<class 'int'\>
What's happening here?
- First, we store text in a variable called
name
- Then, we store the number 15 in a variable called
age
- We use
print()
to show what's inside our variables tip Python lets you create multiple variables in one line!
student1, student2, student3 = "Iman","Ezekiel","Alex"
print(student1)
print(student2)
print(student3)
Expected output:
Iman
Ezekiel
Alex
What's happening here?
student1
, "Ezekiel" goes to student2
, and "Alex" goes to student3
name = "My Name is Chong Wei"
age = 15
print(name)
print (age)
Expected output:
My Name is Chong Wei
15
print(type(name))
Expected output:
\<class 'str'\>
print(type(age))
Expected output:
\<class 'int'\>
What's happening here?
type()
function tells us what kind of data is stored in our variable<class 'str'>
means string (text)<class 'int'>
means integer (whole number)minutes = 36
percentage = (minutes*100) / (minutes*20)
print(percentage)
Expected output:
5.0
What's happening here?
calculate = 2+2**3
print(calculate)
Expected output:
10
What's happening here?
output = "awesome"
print("Learning in Telebort is " +output)
Expected output:
Learning in Telebort is awesome
What's happening here?
+
operator joins two strings togetheroutput = 1
print("Telebort is number " + output)
Expected output:
TypeError Traceback (most recent call last)
\<ipython-input-2-2789aa771edd\> in \<module\>()
1 output = 1
----> 2 print("Telebort is number " + output)
TypeError: must be str, not int
What went wrong?
+
:memo: Note Solution: Convert the number to a string first!
output = 1
print("Telebort is number " + str(output))
Expected output:
Telebort is number 1
What's happening here?
str()
converts the number 1 into the text "1"cakes = int (input("How many cakes do you have? "))
biscuits = int (input("How many biscuits do you have?"))
total_sweets = cakes + biscuits
print(total_sweets)
Expected output:
24
What's happening here?
int()
converts text numbers into real numbers:warning: Warning Without
int()
, Python would combine "10" + "14" = "1014" (joining text, not adding numbers!)
Key Takeaways:
str()
and int()
Code with AI: Try using AI to work with variables and data types.
Prompts:
Try these challenges:
:bulb: Tip Remember: Practice makes perfect! Try changing the values in the examples above and see what happens.