SHARJAH INDIAN SCHOOL, JUWAIZA
GRADE 9 WORK EDUCATION NOTES
INTRODUCTION TO PYTHON
• Python is an interpreted high-level programming language that supports GUI
programming.
• It was developed by GUIDO VAN ROSSUM in 1991.
Syntax
It is the set of rules that defines how a Python program will be written and interpreted.
BASIC MODES OF PYTHON
1. Interactive mode
Python provides an interactive shell to execute code immediately and produce output
instantly. In interactive mode, type the command in front of the Python command prompt
(>>>)
2. Script mode
In the Script mode of working, Python instructions are stored in a file generally with the .py
or .pyw extension and are executed together in one go as a unit. The saved instruction is
known as Python script or Python program.
Click on File in Python shell -> Click New Window-> Type the python codes->Save the file
(by clickingFile->Save As) -> Run the code (click Run module or F5)
Python Tokens
The Smallest individual units in Python programs are known as Tokens.
Python has mainly 5 types of tokens which are given below -:
1. Keywords
2. Identifiers
3. Operators
4. Literals
5. Punctuators
Keywords- Keywords are the reserved words having special meaning and purpose in Python.
Eg:- true, false, if, and, break.
Identifiers- An identifier is a name given to a variable. It helps to differentiate one
entity from others.
Python Variable Name Rules
• A variable name must start with an alphabet (capital or small) or an
underscore character
• A variable name cannot contain spaces.
• A variable name can contain any number of letters, digits, and underscore.
• A Python keyword cannot be used as a variable name.
• Special characters aren’t allowed in variable names except for underscores.
Which of the following variable names are valid/invalid?
(a)123 Hello-Invalid
(b) Sum- Valid
(c) abc@123 - Invalid
(d) 2myvar - Invalid
(e) my-var - Invalid
(f) MYVAR- Valid
(g)_name – Valid
OPERATORS
An operator is a symbol or letter that causes the compiler to take any action and yield a
value. An operator acts on different data items called operands. The various operators are
1. Arithmetic Operators
2. Relational Operators
3. Assignment Operators
4. Logical Operators
5. Membership Operators
6. Identity Operators
Arithmetic
Operator Meaning Example
+ Addition 2+5=7
- Subtraction 10-2=8`
* Multiplication 2*3=6
/ Division 10/2=5
// Floor division [Divide and 5 // 2 = 2
round the result down to the
nearest whole number]
% Return remainder after 9%2=1
division
** Exponent 4**2=16
2. Relational
The relational operators are also known as comparison operators, their main function is to return
either a true or false based on the value of operands.
Operator Description Syntax
Greater than: True if the
left operand is greater than
> the right x>y
Less than: True if the left
operand is less than the
< right x<y
Equal to: True if both
== operands are equal x == y
Not equal to: True if
!= operands are not equal x != y
Greater than or equal to
True if the left operand is
greater than or equal to the
>= right x >= y
Less than or equal to True
if the left operand is less
<= than or equal to the right x <= y
3. Assignment operator
Assignment operators are used for assigning the value of the right operand to the left operand. =
is the most used assignment operator in Python.
Eg: a=10
Sample programs
Write a program to print the sum of 2 integer numbers
a=10
b=20
c=a+b
print(“SUM=”, c)
Write a program to print the product of 2 floating-point numbers
a=10.5
b=20.5
c=a*b
print(“SUM=”, c)
Write a program to compare two numbers. (check whether the first number is less than
the second number)
a=10
b=20
print(a<b)
Output
True.
Literals
Literals in Python are defined as the raw data assigned to variables or constants while
programming.
Various types are string literals, numeric literals, Boolean literals, Floating points, and a
special literal None
String Literals
It is a sequence of characters surrounded by quotes (single, double, or triple).
Eg:- “Aman” , ‘12345’
A character literal is a single character surrounded by a single or double quote
Eg:- ‘a’ , ‘2’
Integer literals
Integer literals are numbers that do not have a decimal point or an exponential part.
Eg: 12 , 25, 100
Floating-point literal
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent
part. You can represent floating point literals either in decimal form or exponential form.
Eg. (Decimal form)- 20.5, 36.8
Eg. (Exponential form) – 2.5E5, 23E9
Boolean literal
Python literal Boolean has two values. One is True, and another one is False.
>>>6<10
>>>True
>>>20>=40
>>>False
None literal
The 'None' literal is used to indicate something that has not yet been created. It is also used
to indicate the end of lists in Python.
>>> v=None
>>>print(v)
>>>None
DATA TYPES
Data types are the classification or categorization of data items. It represents the kind of value
that tells what operations can be performed on a particular data.
2 types of data types are
Primitive - Primitive data types contain simple values of data
Non-primitive- non-primitive data types can store a collection of values in various formats.
Data type
Primitive Non-primitive
String Boolean
Number
Integer
Floating Point
Complex List Diction Tuple Sets
ary
Primitive data types
1)Numbers
1. Integer
Whole numbers ( -ve or +ve )
2. Floating point
Real numbers that contain a decimal point.
3.Complex numbers
Numbers that contains a real and imaginary parts.
General Form_- X+Yj
Eg:- 1+3j
2)Boolean
The Boolean type contains only 2 values – True and false.
3)String
String is a sequence of characters represented in quotation marks.
Eg: “ Python”, “School”.
Various String operations
1. Concatenation of string:
The concatenation operators combine two strings to form one string by appending the second
string to the right-hand end of the first string. The + operator adds a string to another string.
Eg:
>>>a=‘Python’
>>>b=‘Programming’
>>>print(a+b)
>>>PythonProgramming
2. Finding length of the string
The len() is used to find the length of the string.
Eg:
>>>S1= ‘Hello python’
>>>print(len(S1))
>>12
3. Finding the character in a position
- The Python index() method helps you find the index position of an element or an item in a
string of characters or a list of items.
Eg:
>>>S1= ‘Hello python’
>>>print(S1.index(“H”))
>>>0
>>>print(S1.index(“p”))
>>>6
Type casting
Typecasting (or type conversion) is a method of changing an entity from one data type to
another. It is used in computer programming to ensure variables are correctly processed by a
function.
An example of typecasting is converting an integer to a string.
• int(v): is used to convert any value “v” to a plain integer
• float(v): is used to convert any value “v” to a floating-point value
• str(v): is used to convert any value “v” to a string value
Input function
• The input() function reads a line from the input (usually from the user), and converts the
line into a string
• Name=input(“Enter your name”)
How to read integer number in python
• The input() function returns string data and it can be stored in a string variable. Then use
the int() function to change it into an integer value.
• Eg: num= int(input(“Enter a number”))
How to read FLOAT number in python
• The input() function returns string data and it can be stored in a string variable. Then use
the float() function to change it into a floating point value.
• Eg: num= float(input(“Enter a number”))
Sample Programs
1. Write a program to print your school’s name
School_name=input(‘Enter your school name’)
print(‘My school name is’,School_name)
2. Write a program to print your full name which contains first name, middle name, and last
name [Read the first name, middle name, and last name from user]. Also find the length
of your full name.
Fname=input(‘Enter your first name’)
Mname=input(‘Enter your Middle name’)
Lname=input(‘Enter your Last name’)
Fullname= Fname+’ ‘+Mname+’ ‘+Lname.
print(‘ My name is ‘, Fullname)
print(‘ Length= ‘, len(Fullname))
3. Write a program to find out the sum of two integer numbers
num1=int(input(‘Enter 1st Number: ‘))
num2=int(input(‘Enter 2nd Number: ‘))
s=num1+num2
print(‘Sum=’, s)
4. Write a program to find out the product of three float numbers.
num1=float(input(‘Enter 1st Number: ‘))
num2=float(input(‘Enter 2nd Number: ‘))
num3=float(input(‘Enter 3rd Number: ‘))
p=num1*num2*num3
print(‘Product =’, p)
5. Write a program to find the simple interest.
[SI= (P*R*T)/100
P = Principal, R = Rate of Interest in % per annum, and T = Time]
principal =float(input(‘Enter Principal Amount: ‘))
rate=float(input(‘Enter Rate of Interest : ‘))
time =float(input(‘Enter duration : ‘))
si=( principal * rate *time)/100
print(‘Simple interest=’, si)
if Statement of Python
The ‘if’ statement of python is used to execute statements based on conditions. It tests the condition
and if the condition is true, it performs a certain action, we can also provide action for a false
situation.
if statement in Python is of many forms: if, if-else, Elif, nested if, and nested if-else
statements
1. Simple “if”
It decides whether certain statements need to be executed or not. It checks for a given condition,
if the condition is true, then the set of code present inside the” if” block will be executed otherwise
not.
Points to remember with “if”
• It must contain a valid condition that evaluates to either True or False
• Condition must be followed by Colon (:), it is mandatory
• Statement inside if must be at same indentation level.
EXAMPLE _1: Input monthly sales of employee and give a bonus of 10% if the sale is more than
50000 otherwise the bonus will be 0
bonus = 0
sale = int(input("Enter Monthly Sales:"))
if sale>50000:
bonus=sale * 10 /100
print("Bonus = " ,bonus)
EXAMPLE _2: Write a program to check a number is positive.
number= int(input("Enter a number:"))
if(number>0)
print(‘The number is positive’)
print(‘End of the program’)