Handout
2023-2024
Name: Subject: Computer Science
Grade: 6 Topic: Python Programming
Python Programming Language
❖ Input
The input() function allows user input.
x = input('Enter your name:')
print('Hello, ' + x)
❖ Output
The Python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:
x = "awesome"
print("Python is " + x)
Output: Python is awesome
❖ Variables:
Variables are containers for storing data values.
A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type and can even change type after they have been
set.
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for Python variables:
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
Handout
2023-2024
● A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
● Variable names are case-sensitive (age, Age and AGE are three different variables)
❖ Data Types
Variables can store data of different types, and different types can do different things.
In Python, the data type is set when you assign a value to a variable:
Example:
x = "Hello World"
x = 20
x = 20.5
x = True
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Example
Integers:
x=1
y = 35656222554887711
z = -3255522
Float
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
Example
Floats:
x = 1.10
y = 1.0
z = -35.59
Python Strings
String literals in python are surrounded by either single quotation marks or double quotation marks.
'hello' is the same as "hello".
Handout
2023-2024
Multiline Strings
You can assign a multiline string to a variable by using three quotes:
Example
You can use three double quotes:
a = """Hello
How are you?
I am fine."""
print(a)
Boolean Values
In programming, you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two answers, True or False.
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
Example
print(10 > 9) - prints True
print(10 == 9) - Print False
print(10 < 9) - Print False
❖ Python Operators
Operators are used to perform operations on variables and values.
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator Name Syntax Example x=6, y=4
+ Addition x+y 10
- Subtraction x–y 2
* Multiplication x*y 24
Handout
2023-2024
/ Division x/y 1.5
% Modulus x%y 2
** Exponentiation x ** y 1296
// Floor division x // y 1
Python Comparison Operators
Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Handout
2023-2024
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Description Example
and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, return not(x < 5 and x < 10)
False if the result is true
❖ Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
● Equals: a == b
● Not Equals: a != b
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
Example If statement:
a = 33
b = 200
if b > a:
print("b is greater than a")
Handout
2023-2024
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define the scope in the code. Other
programming languages often use curly brackets for this purpose.
Example
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".
Example
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to
screen that "a and b are equal".
Else
The else keyword catches anything which isn't caught by the preceding conditions.
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we
go to the else condition and print to screen that "a is greater than b".
Handout
2023-2024
You can also have an else without the elif:
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
And
The and keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b, AND if c is greater than a:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or
The or keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b, OR if a is greater than c:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Handout
2023-2024
Nested IF statements (Nesting)
● Nested if statements are decision-making / conditional/ selection statements, used when we need to
apply multiple conditions to make one decision, and those conditions are dependent on each other.
● Nested IF conditional statements are used in Python when a situation leads to multiple decisions within
any main decision.
● To avoid the inefficiency of multiple IF statements, it is possible to have one or more IF statements
within one another. This is known as “Nesting”.
● Any number of these statements can be nested inside one another.
● Indentation is the only way to figure out the level of nesting in Python.
Syntax
The colon: after the condition is required. These statements must be indented.
Control Flow
Handout
2023-2024
Note: The syntax of the Nested IF statements depends on the respective problem statement and the logical
approach chosen. However, certain examples of common combinations of Nested IF conditional statements
are given below.
Handout
2023-2024
Program 1:
#Program to check whether the given number is positive, negative, or 0.
number = 5
# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')
# outer else statement
else:
print('Number is negative')
Output:
Number is positive
Program 2:
#Program to check the age and suggest eligibility for work
age = int(input("Please Enter Your Age Here: "))
if age < 18:
print("You are not Eligible to Work ")
else:
if age >= 18 and age <= 60:
print("You are Eligible to Work ")
else:
print("You are too old to work as per the Government rules")
Input / Output:
Please Enter Your Age Here: 12
You are not Eligible to Work
Handout
2023-2024
Program 3:
# Python program to find the value of the expression
var = int (input("Enter a number within 100: "))
if var <= 100:
print ("Expression value is less than 100")
if var == 75:
print ("Which is 75")
elif var == 50:
print ("Which is 50")
elif var == 25:
print ("Which is 25")
elif var < 25:
print ("Expression value is less than 25")
else:
print ("Could not find true expression")
print ("Good bye!")
Input:
75
Output:
Expression value is less than 100
Which is 75
Good bye!
Program 4:
#Program to check which numbers are the same and which are different out
of three numbers
a = int (input ("Enter the first number: "))
b = int (input ("Enter the second number: "))
c = int (input ("Enter the third number: "))
if (a == b):
if (a == c):
print ("All numbers are equal")
if (a!= c):
print ("First and Second number are same but not the Third one")
elif(b==c):
print ("Second and Third number are same but not the First one")
else:
print ("All numbers are different")
Handout
2023-2024
Input / Output:
Enter the first number: 10
Enter the second number: 10
Enter the third number: 20
First and Second number are same but not the Third one
Program 5:
# Program to find the average marks of 3 tests and find the result/grade
m1 = int(input("Enter Marks - Test 1: "))
m2 = int(input("Enter Marks - Test 2: "))
m3 = int(input("Enter Marks - Test 3: "))
total = m1 + m2 + m3
average = total / 3
print("Total: ", total)
print("Average: ", average)
if m1 >= 35 and m2 >= 35 and m3 >= 35:
print("Result: Pass")
if average >= 90 and average <= 100:
print("Grade: A")
elif average >= 80 and average <= 89:
print("Grade: B")
elif average >= 70 and average <= 79:
print("Grade: C")
else:
print("Grade: D")
else:
print("Result: Fail")
Input / Output:
Enter Marks - Test 1: 75
Enter Marks - Test 2: 80
Enter Marks - Test 3: 60
Total: 215
Average: 71.66666666666667
Result: Pass
Grade: C