- cutoff = 7
- cgpas = [8.2, 6.5, 7.4]
- shortlisted = 0
- for c in cgpas:
- if c >= cutoff:
- shortlisted += 1
- print(shortlisted, "shortlisted")
Variables
- cutoff
- 7 (just changed)
The company sets a cutoff.
cutoffEverything in that program is on this page: a box that holds the cutoff, a list of CGPAs, a loop that visits each one and an if that checks it. Every app on your phone, from Instagram to your college portal, is built from these same few ideas.
Quick check
Quick check: The cutoff goes up to 7.5. How many students get shortlisted now?
Variables#
A variable is a named box that holds a value. The name stays the same; what is inside can change. A variable turns yellow on the step that changes it, so watch cgpa on line 3.
- name = "Asha"
- cgpa = 7.8
- cgpa = 8.1
- print(name, cgpa)
Variables
- name
- "Asha" (just changed)
Make a box called name and put the text "Asha" in it.
Every value has a type, which decides what you can do with it:
| Type | Examples | Used for |
|---|---|---|
| Integer | 0, -3, 1000 | Counts, positions |
| Float | 7.8, -0.5 | Decimals, like a CGPA |
| String | "Asha", "42" | Text |
| Boolean | True, False | Yes or no answers |
Quick check
Quick check: x = 5, then x = x + 2. What is x now?
Conditionals#
A conditional lets the program choose. A test like score >= 90 comes out as True or False. An if / elif / else chain checks its tests from the top and runs the branch under the first test that is true. If none is true, the else branch runs.
The colon ends a test, and the indented lines under it are its branch. Python uses that indent, not brackets, to know which lines belong to the if.
Drag the slider and try the edges, like 79 and 80. The code re-runs and lands on the grade.
score- score = 85
- if score >= 90:
- grade = "A"
- elif score >= 80:
- grade = "B"
- elif score >= 70:
- grade = "C"
- else:
- grade = "F"
- print(grade)
Variables
- score
- 80 (just changed)
Set score to 80.
Loops#
A loop repeats a block of code. A for loop runs once for each item in a list: on each pass, the loop variable takes the next value.
- cgpas = [8.2, 6.5, 7.4]
- for c in cgpas:
- print("Checking", c)
- print("Done")
Variables
cgpas
Set cgpas to [8.2, 6.5, 7.4].
A while loop keeps going as long as its test is true. In the code below, total += 10 is short for total = total + 10. Guess first, then step through.
Quick check
Quick check: total starts at 0. While total < 50, add 10. What gets printed?
- total = 0
- while total < 50:
- total += 10
- print(total)
Variables
- total
- 0 (just changed)
Set total to 0.
Functions#
A function is a named piece of code you can run whenever you need it, with different inputs each time. You define it once with def, then call it by name. Watch the lit line: on the call it jumps up into the function, and on return it jumps back with the answer.
- def total_marks(theory, practical):
- total = theory + practical
- return total
- maths = total_marks(62, 25)
- print(maths)
Variables
None yet
Define the function. Its body does not run yet.
Quick check
Quick check: Same function. What does total_marks(40, 18) return?
Lists#
A list keeps many values in order under one name. Each value sits at a position called its index, and counting starts at 0. A negative index counts back from the end.
cgpas[0] is 8.2, cgpas[2] is 7.4, and len(cgpas) is 3. There is no cgpas[3]: asking for it stops the program with an IndexError.
Quick check
Quick check: What is cgpas[-2]?
Putting it together: FizzBuzz#
The classic first coding question: print the numbers 1 to 15, but print Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both.
It needs one new operator. % gives the remainder after dividing: 17 % 5 is 2, because 17 is three 5s with 2 left over. A number is a multiple of 3 exactly when i % 3 == 0.
Quick check
Quick check: What is 12 % 5?
Here is the check for one number. Drag i from 1 to 15 and see which line fires. In the full program, a for i in range(1, 16): loop runs this for every number; range(1, 16) counts from 1 up to 15, stopping before 16.
i- i = 15
- if i % 15 == 0:
- print("FizzBuzz")
- elif i % 3 == 0:
- print("Fizz")
- elif i % 5 == 0:
- print("Buzz")
- else:
- print(i)
Variables
- i
- 15 (just changed)
Set i to 15.
Quick check
Quick check: Why must the i % 15 check come before the i % 3 check?
The same ideas in five languages#
The ideas carry over to every language. The spelling changes, and a few details do too: C has fixed-size arrays instead of lists, and Go writes its while loop with the for keyword.
One last check#
Quick check
Quick check: cutoff = 8. For each c in [7.9, 8.0, 9.1], if c >= cutoff, add 1 to count. What is count?
Next up is star patterns, where a loop inside a loop draws shapes. It is the best way to get comfortable with loops before real algorithms.