GRADE 9- PYTHON Notes
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
Applications of Python:
Program 1: Write a program to input name from the user and display it to the user
Program 1 Output:
# - Single line
“”” ‘’’
Text here or text here – multiline comments
“”” ‘’’
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.
Program 3: Write a program with single and multivariable declaration and print the variables.
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.
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:
Program 5 Output:
Note: Other arithmetic operations be performed similarly
Program 6 Output:
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.
num = 3
if num > 0:
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.
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
Body of if
else:
Body of else
marks = 60
else:
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:
Body of for
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.
list2 = [1, 5, 7, 9, 3]
Program 9 Output:
Program 10: Write a program to find the length, minimum and maximum
values of the list
list = [1, 5, 7, 9, 3]
Program 10 Output:
a = 200
b = 33
if b > a:
elif a == b:
else:
Program 11 Output:
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
Program 13 Output: