GRADE 9- PYTHON Notes
Topics:
1. Simple programs with input and output functions
2. Comments- Single and Multi Line
3. Variables - Rules of variable declaration, Creating readable variable names
4. Data types- String: str, Integer: int, Float: float
5. Operators- Arithmetic (Add, Substract, Multiply, Divide and Remainder), Comparison
(<,>,<=,>=,!=)
6. Expressions
7. Flow controls - if, if-else, elif
8. Loop- for
9. Lists - Creation, Minimum: min , Max: max, Length: len
Why python for AI?
Applications of Python:
1. Input and output functions
Output function: The print() function prints the specified message to the screen, or other standard
output device.
Input function: The input() function allows user input. Prompt parameter can be used to write a
message before input.
Program 1: Write a program to input name from the user and display it to the user
print('Enter your name:')
name= input()
print('Hello, ', name)
Or - using a prompt parameter
name = input("Enter your name:")
print("Hello, ",name)
Program 1 Output:
2. Comments: Single and Multiline
A comment is text that doesn't affect the outcome of a code, it is just a piece of text to let
someone know what you have done in a program or what is being done in a block of code.
# - Single line
“”” ‘’’
Text here or text here – multiline comments
“”” ‘’’
Program 2: Write a program and add a single and multiline comment
name = input("Enter your name:") #using prompt parameter
print("Hello, ",name)
"""
This is a comment
written in
more than just one line
"""
print("Greetings of the day!")
Program 2 Output:
3. Variable: A variable is a named location used to store data in the memory. It is helpful to think
of variables as a container that holds data which can be changed later throughout programming.
➔ A variable name must start with a letter or the underscore character
➔ A variable name cannot start with a number
➔ 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)
➔ A variable name cannot be any of the Python keywords like print, input, if etc
➔ Name the variables in readable format.
Example- myVariableName, MyVariableName, my_variable_name
Program 3: Write a program with single and multivariable declaration and print the variables.
a=20 #Single variable declaration
b,c= 15.6, “Hello” #Multiple variable declaration
print(a)
print(b)
print(c)
Program 3 Output:
4. Data type is the type of value that a variable can store.
Data types: Integer- int, String- str, Float-float
Program 4: Write a program to declare and print the variable with data types integer, String and
Float values
Refer to Program 3. Data types declared and printed in the program are below.
Variable Value Data Type
Name
a 20 Integer
b 15.6 Float
c ‘Hello’ String
5. Operators (Arithmetic and Comparison):
Arithmetic operators used to perform mathematical operations on numerical values are:
Operator (Name) Operation Expression Result
+ (Plus) Addition 10+30 40
- (Minus) Subtraction 9-4 5
* (Asterisk) Multiplication 3*6 18
/ (Back Slash) Division 15/3 5.0
% (Modulo) Remainder 25%10 5
Comparison Operators and its output values are:
Operator Meaning Expression Result
< Less than 20<10 False
10<20 True
> Greater than 6>3 True
3>6 False
<= Less than or equal to 13 <= 24 True
13<=13 True
13 <= 12 False
>= Greater than or equal 45 >= 45 True
to 23 >= 34 False
23>=15 True
!= Not equal to 67 != 45 True
67 != 67 False
== Equal to 5==5 True
5==6 False
Program 5: Write a program to perform sum of two numbers
x = 5
y = 3
print(x + y) # x+y is called an expression. It returns the sum of
x&y
Program 5 Output:
Note: Other arithmetic operations be performed similarly
6. Expressions: Expression is a combination of values/variables and functions/operations that
are combined and interpreted by the compiler to create a new value. It returns a value after
performing the operation/function
Program 6: Write a program to find the greatest of two numbers
x = 5 # Here = is called Assignment operator
y = 3
print(x > y) # x>y is called an expression.
Program 6 Output:
Note: Other comparison operations be performed similarly
7. Flow Control :
7.1. if statement : The if statement is used to check a condition: if the condition is true, we run a block of
statements (called the if-block). If the text expression is False, the statement(s) is not executed.
SYNTAX:
if test expression:
statement(s)
Note: In Python, the body of the if statement is indicated by the indentation. Body starts with an
indentation and the first unindented line marks the end.
Program 7: Write a program to check if the number is positive or negative
num = 3
if num > 0:
print(num, “is a positive number.”)
print("this is always printed")
Program 7 Output:
7.2: if…else statement: The if..else statement evaluates test expression and will execute body of if only
when test condition is True. If the condition is False, body of else is executed. Indentation is used to
separate the blocks.
SYNTAX:
if test expression:
Body of if
else:
Body of else
7.3. If..elif..else statement : The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, the body of else is executed.
Only one block among the several if...elif...else blocks is executed according to the condition.
The if block can have only one else block. But it can have multiple elif blocks.
SYNTAX:
if test expression:
Body of if
elif test expression:
Body of if
else:
Body of else
Program 8: Write a program to find the grade of the student
marks = 60
if marks > 75:
print("You get an A grade")
elif marks > 60:
print("You get a B grade")
else:
print("You get a C grade")
Program 8 output:
8. for loop: The for..in statement is a looping statement which iterates over a list of items i.e. go
through each item in an order.
SYNTAX:
for val in sequence:
Body of for
Program 9: Write a program to print alphabets in a word using for
loop
for x in "banana":
print(x)
Program 9 Output:
9. Lists: Lists are used to store multiple items(allows same or multiple data types) in a single
variable. These are indexed/ordered. List items index starts from 0
9.1. Creation: It is created by placing all the items (elements) inside a square bracket [ ], separated by
commas.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
list4 = ["abc", 34, True, 40, "male"]
Program 9: Write a program to create a list and print the list
elements
student_height_weight = ["Ansh", 5.7, 60]
print(student_height_weight)
Program 9 Output:
9.2: Built-in Functions of list:
len()- returns the length of the list
min()- returns the minimum value in the list
max()- returns the maximum value in the list
Program 10: Write a program to find the length, minimum and maximum
values of the list
list = [1, 5, 7, 9, 3]
print(len(list)) # length of the list
print(min(list)) # minvalue in the list
a= max(list) #max value is stored in the variable a
print(a) # maxvalue in the list
Program 10 Output:
Other sample programs for reference:
Program 11: if, elif, else
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")
Program 11 Output:
Program 12: For loop (check indentation)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Program 12 Output:
Program 13: Write a program to input two numbers from the user and
find the sum of them
x = input("Enter first number ")
y = input("Enter second number ")
sum = int(x) + int(y)
print("The sum is: ", sum)
Program 13 Output: