Python Familiarisation
_______________________________________________
Access Jupyter lab online:
To use Jupyter online: [Link] (Click on Python (Pyodide) )
_______________________________________________
Part 1: Basics
1. Print:
print("Hello, Python!") # Prefix hash (#) symbol to add comment.
_______________________________________________
2. Simple calculations:
# Addition
print(5 + 3)
# Multiplication
print(4 * 2)
# Division
print(11 / 4) # float result
print(11 // 4) # integer division
print(11 % 4) # To get the remainder only.
Exercises:
Write Python expressions to calculate:
The area of a rectangle (length × width).
The average of three numbers.
_______________________________________________
3. Variables and Data Types
name = "Your name" # string
age = 21 # integer
height = 172 # float
is_student = True # boolean
print(name, age, height, is_student)
To find out the data type of a variable:
type(age)
Common data types:
int → integers
float → decimal numbers
str → text
bool → True/False
Exercise 2:
Create variables for your:
Name,
age,
favorite number, and
whether you like biriyani? (yes or no)
_______________________________________________
4. Receiving variable value from the user.
n=input(“Enter a number”)
print(n)
_______________________________________________
5. Strings
Strings are text and are to be written inside quotes.
greeting = "Hello"
print(greeting + " World") # concatenation
# f-strings are useful options to insert values of variables inside a string:
name = "ABC"
age = 100
print(f"My name is {name} and I am {age} years old.")
Exercise 3:
Create a variable for your favourite movie and print:
My favorite movie is [add the movie name here].
_______________________________________________
6. Lists and Indexing
Lists store multiple items.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # first item. Python starts counting from 0 (not 1)
print(fruits[-1]) # Last item
[Link]("mango") # add new item
print(fruits)
Exercise 4:
Create a list of 5 countries you want to visit. Print the first and last.
_______________________________________________
7. Control Flow – If/Else
Control the program’s decisions.
temperature = 30
if temperature > 25:
print("It's hot!")
elif temperature > 10:
print("It's cool!")
else:
print("It's cold!")
Note:
there is a colon after if/elif/else statements.
there is an indentation after the if/elif/else statements. If the intendation is wrong,
it will give an error.
Exercise 5:
Ask the user for their age. If they are 18 or older, print "You are an adult", else "You are a
minor".
8. Loops
Iteratively perform tasks.
# For loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# While loop
count = 0
while count < 3:
print("Hello")
count += 1
Note:
there is a colon after for/while statements.
there is an indentation in the lines within the for/while loops. If the intendation is
wrong, it will give an error.
Exercise 6:
Print numbers from 1 to 10 using a loop.
9. Functions
Functions group code into reusable blocks.
# Function name = greet.
# Argument = name
def greet(name):
return f"Hello, {name}!"
print(greet("Sara"))
Exercise 7:
Write a function that takes a number and returns its square.
9. Modules
Python has built-in modules for different purposes.
import math
print([Link](16))
import random
print([Link](1, 6))
Exercise 8:
Generate a random number between 1 and 100 and print it.
10. Activity – Guess the Number
Combine everything you learned:
import random
secret = [Link](1, 10)
guess = 0
while guess != secret:
guess = int(input("Guess the number (1-10): "))
if guess < secret:
print("Too low!")
elif guess > secret:
print("Too high!")
print("You guessed it!")