Loading the lesson
Loading the lesson
AI & ML Engineering · Programming Foundations · Lesson 4 of 10
Variables, data types, conditions and loops. Enough Python to read and write real scripts.
Read the short notes and pictures.Do everything inside a blue frame.
A program works on values: a name, an age, a price. To use a value again later, you give it a name. That name is a variable.
Python runs your program one line at a time, top to bottom. Step through this one and watch what each line puts in memory.
1name = "Asha"2age = 283height = 1.654is_learner = True5age = age + 16print(name, age)
Press Next to run the first line.
Memory
Nothing yet
Output
str
"Asha"Text. Always inside quotes.
int
28A whole number.
float
1.65A number with a decimal point.
bool
TrueTrue or False.
What type is "42"?
An operator takes values and produces a new one. Comparisons always produce True or False, which is what conditions run on.
Arithmetic
7 // 2 gives 3+ - * / do what you expect. // drops the remainder, % keeps only the remainder.
Comparison
age >= 18 gives True== != < > <= >= compare two values.
Logic
a and band needs both true. or needs one. not flips it.
Text
f"Hi {name}"An f-string drops a value into text.
What does 17 % 5 give?
Python checks each condition in order and runs the first block that is true. The rest are skipped.
1score = 722if score >= 90:3grade = "A"4elif score >= 70:5grade = "B"6else:7grade = "C"8print(grade)
Press Next to run the first line.
Memory
Nothing yet
Output
A for loop takes the items one at a time and runs the indented block once for each.
1total = 02for n in [3, 5, 2]:3total = total + n4print(total)
Press Next to run the first line.
Memory
Nothing yet
Output
for i in range(4): how many times does the block run?
Use for when you have items to go through. Use while when you only know when to stop.
1count = 32while count > 0:3print(count)4count = count - 15print("Go")
Press Next to run the first line.
Memory
Nothing yet
Output
Print the even numbers from 2 to 10, one per line.
Hint: range can count in steps: range(start, stop, step). The stop value is not included.
input() shows a question, waits for the person to type something and press enter, and gives you what they typed. It always gives you text, even when they type a number.
1city = input("City: ")2temp = input("Temperature: ")3temp = float(temp)4print(f"{city} is {round(temp)} degrees")
Press Next to run the first line.
Memory
Nothing yet
Output
Someone types 5 at age = input("Age: "). What is age + 1?
Most small programs are a menu: show the choices, do what was picked, show the choices again. while True: repeats forever, and break leaves the loop.
1while True:2choice = input("1 to say hi, 2 to quit: ")3if choice == "1":4print("Hi")5elif choice == "2":6break7else:8print("Type 1 or 2")9print("Bye")
Press Next to run the first line.
Memory
Nothing yet
Output
Ask for temperatures one at a time until the person types done. Then print how many they typed and the highest one.
Hint: Keep a list. Inside a while True loop, break when the answer is "done", otherwise append float(answer).
Practice: your first Python
Make a variable price set to 250 and a variable qty set to 3. Store their product in total and print it.
Press Run to see the output here.
A lot of real code is about text: names, file lines, messages. A few tools cover most of it.
f-strings
f"Hello {name}, you are {age}"Put values into text. The f before the quote turns it on.
.lower() and .strip()
" Pune ".strip().lower()Tidy text before you compare it. This gives "pune".
.split()
"a,b,c".split(",")Break text into a list of pieces: ["a", "b", "c"].
len()
len("hello")How many characters. It also counts items in a list.
name = "Asha". What does f"Hi {name}!" give?
Keep these