Вы находитесь на странице: 1из 60

GETTING STARTED

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.

 Well, these are words that if we use these


words, we must use them to mean the
thing that Python expects them to mean.
 Each words have their own meaning and
functions that they are expected to do.
 Another way to put that is we can't use
them elsewhere.
Variables in python
 Variables are used to store values of
some kind like numeric,char, decimal etc
 For eg
mark =45.5 # float type
rno=4 #int type
name= „Python‟ #string type
Data type in Python
 int
eg 2,45,67,34
 float
eg 45.3,67.2
 string
eg : “hello”
 boolean
eg TRUE, FALSE
A variable has 3 main components
 Identity of the variable
 Type of the variable
 Value of the variable
Identity of the variable
 It refers to the variables memory address
that has been assigned to the variable
once it is created.
 It can be checked as
 syntax >>> id(variable)
 >>> x=4
 >>> id(x)
 1529795680
Value of the variable
 Variable provide a means to name values
so that they can be used and manipulated
later.
 To bind value to a variable, we use
assignment operator(=). This process is
also termed as building of a variable
 eg
 marks = 78
 print(marks)
 87= marks # this is an error
Type of the variable
 It refers to the data type of the variable
 That means what type of data is stored in
the variable
 syntax : type(variable)

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

>>> print("Your name is :",name)


Your name is : Python

>>> print("Your number is ",x," and your


name is ",name)
Your number is 10 and your name is Python
Multiple assignments
a=b=10
print(a,b)

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)

You will get an error, since a variable is not


defined, A variable is not created until some
values is assigned to it.
What will be the output?

You will get an error, since a variable is not


defined, A variable is not created until some
values is assigned to it.
To find the type of variable
x=20
print(type(x))
name ='Python'
print(type(name))
mark=45.5
print(type(mark))
grade='D'
print(type(grade))
Dynamic typing
x=10
the variable x is storing an int value
x=“hello”
Now the variable x will be storing a string
value
In python, a variable pointing to a value of
a certain type can be made to point to a
value of different type, this is called as
Dynamic typing.
Predict the output <class 'str'>
ch=“Hello” <class 'int'>
<class 'float'>
print(type(ch)) #1 <class 'int'>
<class 'float'>
ch=34 <class 'bool'>

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)

Enter the roll number 9


Roll number = 9
x=input("Enter the roll number ")
print("Roll number =",x)
x=x+5
print(x)

 # ERROR
x=input("Enter the roll number ")
print("Roll number =",x)
print(type(x))

Enter the roll number 9


Roll number = 9
<class 'str'>
 Strings cannot be used for arithmetic
operations or any other numeric
operations
 For these we have to use only int or float
The problem can be solved in 2 ways:
Method 1:
x= input(“Enter your roll number”)
x=int(x)
x=x+2
print(x)
Method 2
x= int(input(“ Enter your roll number ”))
x=x+2
print(x)
Write the stmt to get the foll output.

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

Вам также может понравиться