Class 8 PYTHON
Class 8 PYTHON
INTRODUCTION TO PYTHON :
● Python is a high-level computer programming
language which is very popular among
programmers.
● It was created by Guido van Rossum in 1989, and
first version of python was released in 1991.
● Python is a
○ Case sensitive language
○ Platform independent language
○ Easy to use, read and understand
○ Free to use
PYTHON IDLE :
● It stands for integrated development and
learning environment .
● The commands are written in front of the python
command prompt ‘>>>’ .
● >>> symbol on python IDLE shows that the python
shell is ready to take commands from the user and to
execute it .
● We just need to enter the command and press the
enter key .
Interactive mode Script mode
iii) It automatically displays the output just It does not display the result automatically .
after entering one line of input.
iv) It takes less time to write and execute the It takes more time to write and execute the
program . program .
v) the input and output statements are input and output statements are shown
compressed together . separately.
vi) programs written in interactive mode can We can save and edit the programs and use
not be saved and edited . them later. The Python file is saved with
extension “.py” .
1. Operators are the
symbols which are
used to perform any
operation in
expression.
2. Operands are the
values which are
involved in an
expression.
print() :
● Python uses print() to display the user defined
message or output of any program on the screen .
Syntax :
print(“message to be displayed”)
Or
print(‘message to be displayed’)
● Starting and ending quotes of the message should be
same .
input() :
● Python uses input() to accept the value as
string(default) from user and store it in the variable .
Syntax :
Variable_name = input(“message”)
Example :
name=input(“enter your name:”)
● There are two functions used with input() to convert
the data type which are int() and float() .
● int() is used to convert a value into integer.
● float() is used to convert a value into float .
Python Variable :
● Variable is a computer memory location in which
we can store the values to be used later in the program .
● A variable can store only one data at a time.
● If a new value is assigned to a variable, the previous
value gets overwritten .
● Syntax :
Variable-name = value
Example :
A = 34
B = 65.9
C = “ hello ”
Rules to write the variable name:
● Starting -> either alphabet(a-z,A-Z) or underscore
symbol ( _ )
● Consists of -> alphabet(a-z,A-Z), underscore ( _ ) ,
digits ( 0-9 )
● Special characters (symbols) are not allowed.
● Space is also not allowed .
● It can be of any length. It means the variable name
can have as many letters as the user wants.
● Case sensitive (age and Age are different variables .)
Data types
Integer String
( int ) Float
( str )
Conditional
statement
if if…else if…elif…else
statement statement statement
1. if statement :
● It is used to evaluate only one condition.
● If the output of the condition is true, it will
execute the if-block statements followed by
it.
● If the output of condition is false , it will skip
statements.
● Syntax :
if(condition):
if-block statement(s)
2. if…else statement :
● It is also used to evaluate only one condition.
● If the output of the condition is true, it will
execute the if-block statements followed by it.
● If the output of condition is false , it will execute
the else-block statements .
● Syntax :
if(condition):
if-block statement(s)
else :
else-block statement(s)
3. if…elif…else statement :
● It is also used to evaluate more than one
condition.
● Whichever condition will be true, statements
followed by that condition will be executed.
● If no condition is true , it will execute the
else-block statements .
● Elif statement can be written as many times the
user wants to check condition.
Syntax of if…elif…else statement :
if(condition 1):
Statement 1
elif(condition 2) :
Statement 2
.
.
.
.
else :
else-block statement(s)
Looping statement :
● While writing program codes, we need to repeat a
statement to get the desired results. There comes the
concept of loops in programming.
● In Python, the iterative statements are also known as
looping statements or repetitive statements. The
iterative statements are used to execute a part of the
program repeatedly as long as a given condition is
True. When the condition becomes false, the loop
terminates.
● Loops helps us to write the same program in fewer
lines which saves time, memory space and effort also.
1. For loop :
● When we already know how many times a loop
body will run, then we use for loop there.
● It is also known as a definite loop.
● Syntax :
for variablename in range(initial,final,step) :
Loop body statement
Range function / range() :
Syntax of range() :
range(initial value, final value, step value)
Example:
i=1
while ( i<5):
print(i)
i=i+1
Infinite loop :
● An infinite loop is also known as an endless
loop , meaning the loop will never end.
● It is a sequence of instructions in a program
which loops endlessly.
● It happens either the provided condition can
never meet or loop has not any terminating
condition .
Infinite loop :
i=1 i=1
while(i<7) : while(i>0) :
print(i) print(i)
● In the above programs , the condition will always
be true and the loop will be executed again and
again .