Академический Документы
Профессиональный Документы
Культура Документы
WITH PYTHON
CHAPTER I
Python language
developed by Guido Van Rossum in feb
1991
Python is based on the influence of two
programming language
a) ABC Language
b) Modula-3
It is an Object oriented programming
language
Advantages of Python
Easy to use :
Very simple syntax rules, It is a high level
programming language and a very programmer
friendly language
Expressive language :
Needs only very few lines of code to get
the work done.
Interpreted Language :
It uses an interpreter and executes the
code line by line.
Its Completeness :
When you install python, you get all the
libraries and built in functions along with it.
Cross platform language:
Python can run equally on all the variety of
platform like windows, Linux, Macintosh,
smartphones etc.
Free and open source:
It is available freely, also its source code is
open source.
Variety of usage and applications:
Python is used in a variety of fields like
scripting, game development, GUI programs,
Database applications etc.
Disadvantages
Not the fastest language :
Python is an interpreted language not a
fully compiled one. It is first semi-compiled
into internal byte code which is then
exerted by a python interpreter.
Lesser libraries than C++, Java
Not strong on type binding :
Python interpreter is not very strong on
catching „type-mismatch „ issues.
Not easily convertible
Interpretation Vs Compilation
Once you have installed Python on your
computers, you can work with Python in
two different ways:
a) In interactive mode ( also called
immediate mode)
b) In script mode
Interactive mode
The interactive interpreter of Python is also
called as Python shell
The interactive shell is also interactive in
the way that it stands between the commands
or actions and their execution. This means
the Shell waits for commands from the user,
which it executes and returns the result of the
execution.
Interactive mode proves very useful in testing
code
You type the commands one by one and get the
result one by one.
Script mode
If you want to save all the commands in
the form of program file and want to see
all the output lines together rather than
individual successive commands we have
to use script mode
Comments in Python
Comments in Python can be given with
the starting symbol #
Multiline Comment
"""This is a
multiline docstring.""“
print("Hello, World!")
print command
>>>print(3+4);
7
>>>print(4*9);
36
>>> 5/2
2.5
>>> print(5%3);
2
>>> print(5*9,7+9);
45 16
PYTHON FUNDAMENTALS
Python character set
Letters : A-Z , a-z
Digits : 0-9
Special Symbols : @,$,! etc
White spaces : blanks , tabs , carriage
return
other characters :
Tokens
The smallest individual unit in a program
is called token or lexical unit.
Python has the following tokens
a) keywords
b) Identifiers
c) Literals
d) Operators
e) Punctuators
Rules for identifiers:
It can be a combination of letters in
lowercase (a to z) or uppercase (A to Z)
or digits (0 to 9) or an underscore.
It CANNOT start with a digit , but
CAN start with _(underscore)
Keywords cannot be used as an identifier.
No special symbols like !, @, #, $, %, +
etc. in identifier.
Commas or blank spaces are not allowed
within an identifier.
Identify the correct identifiers from
the following
a) amount
b) 23amount
c) amount23
d) amount*2
e) am@23
f) am_23
g) am 23
h) _23am
Reserved words.
x=10
>>> type(x)
<class 'int'>
lvalue and rvalue
An expression has two values. Lvalue and
Rvalue.
Lvalue: the LHS part of the expression
Rvalue: the RHS part of the expression
Python first evaluates the RHS of the
expression and then assigns to LHS.
Example:
P =5
Assigning values to variables
x=10
name=„Python‟
>>>print(x)
10
>>>print(name)
Python
>>>print(x,name)
10 Python
>>> print("The number is :",x)
The number is : 10
x,y=10,20
print(x,y)
x,y=a,b+a
print(x,y)
Predict the output
a,b,c = 5,10,7
print(a,b,c)
b,c,a = a+1,b+2,c-1
print(a,b,c)
5 10 7
6 6 12
Q2
p,q=3,5
q,r=p-2,p+2
print(p,q,r,sep='@')
3@1@5
Q3
X=10
Y,Y=X+2,X+5
print(x,y)
10 15
Q4
x,x=10,20
y,y=x+10,x+30
print (x,y)
20 50
What will be the output?
print(x)
x=20
print(x)
print(type(ch)) #2
ch=34 / 3
print(type(ch)) #3
ch=34%5
print(type(ch)) #4
ch= 34/2
print(type(ch)) #5
ch=True
print(type(ch)) #6
1. W.A.P to find the sum and product of
two numbers
2. W.A.P to print true if the first number is
greater than the second number else
false
Simple input and output
To an input from a user, you can use built-
in function input ( ). The function input( )
is used in the following manner:
variable = input (<info to be displayed>)
eg
x= input(“Enter the roll number”)
x=input("Enter the roll number ")
print("Roll number =",x)
# ERROR
x=input("Enter the roll number ")
print("Roll number =",x)
print(type(x))
1. BGS 2. B 3. B G S
NPS G N P S
S
N
P
S
1. To input 2 variables and find their
sum and product
num1 = int( input( "Enter number 1 :"))
num2 = int( input( "Enter number 1 :"))
sum=num1 + num2
prod=num1 * num2
print("sum = ",sum,"\n product =",prod)
Program to perform all mathematical
operations:
+
-
*
/
%
**
//
2. W.A.P to calculate the simple interest by
accepting the value of principal, rate and
time period from user.
3. To find the circumference and area of a
circle
4. To find the sum, product and difference
of any two given number.
5. To find the area of a triangle.
6. To find the Volume of a sphere
7. To find the Volume of cylinder
8. To find the Volume of Cube.
1. W.A.P to enter the user class and section
and display them
2. W.A.P to enter the user age and display his
age after 15 years.
3. W.A.P to accept your marks in three
subject and display them
4. To find the percentage of the student in 5
main subject.(Assume total mark of each
subject is 100).
5. Accept two number and interchange the
values
6. Accept a 2 digit number and reverse it
eg : if the number is 93
The reverse number is 39
x = int(input("Enter a 2 digit number :"))
print(x)
o=x%10
print("o= ",o)
t=int(x/10)
print("t= ",t)
x=o*10+t
print("The reversed number = ",x)
Shorthand assignment operators
Shorthand assignment operator is a
combination of a binary operation and
assignment operator
eg
a=5
a=a+10 or a+=10
a*=20 means a=a*20