Grade 6 - Handout On Python Programming 2023-24
Grade 6 - Handout On Python Programming 2023-24
2023-2024
❖ Input
❖ Output
x = "awesome"
print("Python is " + x)
❖ Variables:
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
● 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
Boolean Values
❖ Python Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
+ Addition x+y 10
- Subtraction x–y 2
* Multiplication x*y 24
Handout
2023-2024
% Modulus x%y 2
** Exponentiation x ** y 1296
// Floor division x // y 1
== Equal x == y
!= Not equal x != y
not Reverse the result, return not(x < 5 and x < 10)
False if the result is true
● 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
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 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:
Output:
Number is positive
Program 2:
Input / Output:
Please Enter Your Age Here: 12
You are not Eligible to Work
Handout
2023-2024
Program 3:
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