0% found this document useful (0 votes)
7 views16 pages

Python - Revision Tour

Python, created by Guido Van Rossum and released in February 1991, is a versatile programming language that supports both procedural and object-oriented approaches. It features various data types, tokens, and operators, and is known for its ease of use, expressiveness, and cross-platform capabilities, though it has some limitations such as speed and library availability. The document also covers key concepts like variables, literals, and program structure in Python.

Uploaded by

johnt1victor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Python - Revision Tour

Python, created by Guido Van Rossum and released in February 1991, is a versatile programming language that supports both procedural and object-oriented approaches. It features various data types, tokens, and operators, and is known for its ease of use, expressiveness, and cross-platform capabilities, though it has some limitations such as speed and library availability. The document also covers key concepts like variables, literals, and program structure in Python.

Uploaded by

johnt1victor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.

com

Python was created by Guido Van Rossum


The language was released in February I991
Python got its name from a BBC comedy series from seventies-
“Monty Python‟s Flying Circus”
 INTRODUCTION Python can be used to follow both Procedural approach and
 TOKENS Object Oriented approach of programming
 BAREBONES OF PYTHON
 VARIABLES AND ASSIGNMENT It is free to use
 INPUT & OUTPUT
 DATA TYPES
Python is based on or influenced with two programming
 MUTABLE AND IMMUTABLE TYPESS languages:
 EXPRESSION ABC language [replacement of BASIC]
 FLOW OF CONTROL
 JUMP STATEMENT Modula-3
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

Easy to use Object oriented language Not the fastest language


Expressive language Lesser Libraries than C, Java, Perl
Interpreted Language
Not Strong on Type-binding
Its completeness
Cross-platform Language
Not Easily convertible
Fee and Open source
Variety of Usage / Applications

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

In a passage of text, individual words and punctuation Keywords are the reserved words and have special meaning for
marks are called tokens or lexical units or lexical python interpreter. Every keyword is assigned specific work and it
elements. The smallest individual unit in a program is can be used only for that purpose.
known as Tokens. Python has following tokens: A partial list of keywords in Python is
Literals

 Keywords # Simple Python Program


 Identifiers(Name) for i in range(1,10):
if i %2 != 0: Punctuators
 Literals print(i)
 Operators Keywords Operators

 Punctuators
Identifier
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

GradePay File_12_2018 JAMES007


GRADEPAY _ismarried _to_update
Are the names given to different parts of program like variables,
objects, classes, functions etc.
Identifier forming rules of Python are :

 Is an arbitrarily long sequence of letters and digits


 The first character must be letter or underscore Grade-Pay 12_2018_File $JAMES007
 Upper and lower case are different if RollNo. Roll No

 The digits 0-9 are allowed except for first character


 It must not be a keyword
 No special characters are allowed other than underscore is allowed.
 Space not allowed

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Literals are data items that have a fixed value. Python • It is a collection of character(s) enclosed in a double or single
quotes. It can be either Single line strings or Multiline Strings
supports several kinds of literals:
• SINGLE LINE STRING : must terminate in one line i.e. the closing
• String Literal quotes should be on the same line as that of the opening quotes
• Numeric Literals • Examples of String literals
• Boolean Literals • “Python”
• Special Literals – None • “Mogambo”
• Literal Collections • „123456‟
• „Hello How are your‟
• „$‟, „4‟,”@@”
• In Python both single character or multiple characters enclosed
in quotes such as “kv”, „kv‟,‟*‟,”+” are treated as same
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• MULTI LINE STRING : To store multiline string Python provides two


ways: Escape Sequence What it does Escape Sequence What it does
• (1) By adding a backslash at the end of normal Single / Double quoted \\ Backslash \r Carriage return
string. For e.g. \‟ Single quotes \t Horizontal tab
>>> Name="1/6 Mall Road \
Kanpur" \” Double quotes \uxxxx Hexadecimal
value(16 bit)
>>> Name
'1/6 Mall RoadKanpur' \a ASCII bell \Uxxxx Hexadecimal
>>> value(32 bit)
(2) By typing text in triple quotation marks \b Back Space \v vertical tab
for e.g. \n New line \ooo Octal value
>>> Address="""1/7 Preet Vihar 1/7 Preet Vihar
New Delhi New Delhi
India
India""" >>> Address
>>> print(Address) '1/7 Preet Vihar\nNew Delhi\nIndia'

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Python determines the size of string as the count of characters in the


string. For example size of string “xyz” is 3 and of “welcome” is 7. • The numeric literals in Python can belong to any of the
But if your string literal has an escape sequence contained in it then following numerical types:
make sure to count the escape sequence as one character. For e.g.
1) INTEGER LITERALS: it contain at least one digit and must
not contain decimal point. It may contain (+) or (-) sign.
String Size
• Types of Integer Literals:
„\\‟ 1
a) Decimal : 1234, -50, +100
„abc‟ 3
„\ab‟ 2 b) Octal : it starts from symbol 0o (zero followed by
“Meera\‟s Toy” 11
letter ‘o’)
“Vicky‟s” 7 • For e.g. 0o10 represent decimal 8
• You can check these size using len() function of Python. For example
• >>>len(„abc‟) and press enter, it will show the size as 3
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• 2) Floating point Literals: also known as real literals. Real


>>> num = 0o10 literals are numbers having fractional parts. It is
>>> print(num) represented in two forms Fractional Form or Exponent Form
It will print the value 8 • Fractional Form: it is signed or unsigned with decimal point
• For e.g. 12.0, -15.86, 0.5, 10. (will represent 10.0)

c) Hexadecimal : it starts from 0x (zero followed by • Exponent Part: it consists of two parts “Mantissa” and “Exponent”.
letter ‘x’) • For e.g. 10.5 can be represented as 0.105 x 102 = 0.105E02 where 0.105
is mantissa and 02 (after letter E) is exponent
>>> num = 0xF
3) Complex number Literals : Complex number in python is made up
>>> print(num) of two floating point values, one each for real and imaginary part.
it will print the value 15 Example
>>> num = 0xABC >>> x = 1+0j
>>> print(num) >>> print x.real,x.imag
it will print the value 2748 1.0 0.0
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

A Boolean literals in Python is used to represent one of the two Python has one special literal, which is None. It indicate absence
Boolean values i.e. True or False of value. In other languages it is knows as NULL. It is also used
These are the only two values supported for Boolean Literals to indicate the end of lists in Python.
For e.g.
>>> salary=None
>>> isMarried=True >>> type(salary)
>>> type(isMarried) <class 'NoneType'>
<class 'bool'>

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Operators can be
• are symbol that perform specific operation when applied on
Type of Operators Symbols
variables. Take a look at the expression:
Arithmetic +, -, *, /, %, **, //
(Operator)
Relational >, <, >=, <=, ==, !=
10 + 25 (Operands)
Logical and, or
Above statement is an expression (combination Identity is, is not
of operator and operands) Assignment =
i.e. operator operates on operand. some operator requires two
Membership in, not in
operand and some requires only one operand to operate
Arithmetic +=, -=, *=, /=, %=, **=, //=
Assignment
Bitwise &, ^, |, <<, >>
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Punctuators are symbols that are used in programming • It means basic structure of a Python program
• Take a look of following code:
languages to organize sentence structure, and indicate
the rhythm and emphasis of expressions, statements, and #This program shows a program‟s component
program structure. # Definition of function SeeYou() follows Comments
def SeeYou():
• Common punctuators are: „ “ # $ @ []{}=:;(),. print(“This is my function”) Function
#Main program
A=10

Statements
B=A+20 Expressions
Inline Comment
C=A+B
if(C>=100) #checking condition
print(“Value is equals or more than 100”)
else: Block

Indentation
print(“Value is less than 100”)
SeeYou() #Calling Function
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Variables are named temporary location used to store • Note: Python variables are not storage containers like other
values which can be further used in calculations, printing programming language. Let us analyze by example.
result etc. Every variable must have its own Identity, type • In C++, if we declare a variable radius:
and value. Variable in python is created by simply radius = 100
assigning value of desired type to them.
[suppose memory address is 41260]
• For e.g
Now we again assign new value to radius
• Num = 100
radius = 500
• Name=“James”
Now the memory address will be still same only value
will change
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Now let us take example of Python: • In Python, a variable declared as numeric type can be
radius = 100 [memory address 3568] further used to store string type or another.
• Dynamic typing means a variable pointing to a value of
radius = 700 [memory address 8546] certain type can be made to point to value/object of
different type.
• Lets us understand with example
Now you can see that In python, each time you assign new
value to variable it will not use the same memory address x = 100 # numeric type
and new memory will be assigned to variable. In python the print(x)
location they refer to changes every time their value x=“KV OEF” # now x point to string type
change.(This rule is not for all types of variables) print(x)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Always ensure correct operation during dynamic typing.


If types are not used correctly Python may raise an
error.
x=100 x int: 100
• Take an example
x = 100
x=“KV OEF” x int:100 y=0
y=x/2
print(y)
string:KV OEF x='Exam'
y = x / 2 # Error, you cannot divide string
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Dynamic typing is different from Static typing. In Static • Python is very versatile with assignments. Let‟s see in how
different ways we can use assignment in Python:
Typing, a data type is attached with a variable when it
1. Assigning same value to multiple variable
is defined first and it is fixed. That is data type of a = b = c = 50
variable cannot be changed in Static typing whereas 2. Assigning multiple values to multiple variable
there is no such restriction in dynamic typing, which is a,b,c = 11,22,33
supported by Python.
Note: While assigning values through multiple assignment,
remember that Python first evaluates the RHS and then
assigns them to LHS

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

x,y,z = 10,20,30 #Statement 1 Let us take another example


z,y,x = x+1,z+10,y-10 #Statement 2
print(x,y,z) y, y = 10, 20

Output will be
In above code first it will assign 10 to y and again it assign 20
10 40 11
to y, so if you print the value of y it will print 20

Now guess the output of following code fragment


Now guess the output of following code
x,y = 7,9
x, x = 100,200
y,z = x-2, x+10
y,y = x + 100, x +200
print(x,y,z)
print(x,y)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• In python we can take input from user using the built-in >>> salary=input("Enter your salary ")
function input(). Enter your salary 5000
• Syntax >>> bonus = salary*20/100
variable = input(<message to display>) Traceback (most recent call last):
Note: value taken by input() function will always be of String type, so by File "<stdin>", line 1, in <module>
default you will not be able to perform any arithmetic operation on variable.
TypeError: unsupported operand type(s) for /: 'str' and 'int'
>>> marks=input("Enter your marks ")
Enter your marks 100
>>> type(marks)
<class 'str'>
Here you can see even we are entering value 100 but it will be treated as
string and will not allow any arithmetic operation.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Now we are aware that input() function value will • Python allows to display output using print().
always be of string type, but what to do if we want • Syntax:
number to be entered. The solution to this problem is to print(*Object [,sep=“string”,end=“string”])
convert values of input() to numeric type using int() or here *Object means one or multiple comma separated
objects/messages to be printed. It convert everything (*Object)
float() function. in String before printing
Example 1
>>> age = int(input(“Enter your age “)) print(“Welcome”)
>>>print type(age) Example 2
<type „int‟> print(100)
>>>salary = float(input(“Enter salary”)) Example 3
>>>print type(salary) Age=20
<type „float‟> print(“Your age is “, Age)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

Example 4 Note:
r = int(input("Enter Radius ")) Be default each print statement appends a new line character
print("Area of circle is ",3.14*r*r) after the printed value. The default value of „end‟ parameter
Example 5 is “\n”
print(„Amar‟,‟Akbar‟,‟Anthony‟) Example
Output will be : Amar Akbar Anthony print(“Learning Python”)
space will be automatically inserted between different
values as separator because the default value of ‘sep’ print(“Developed by Guido Van Rossum”)
parameter is space Output
Example 6 Learning Python
Print(„Amar‟,‟Akbar‟,‟Anthony‟, sep=„**‟) Developed by Guido Van Rossum
Output will be : Amar**Akbar**Anthony
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

Note: Open a new script file and type the following code:
We can change the value of end to any other value. num1=int(input("Enter Number 1 "))
Example
num2=int(input("Enter Number 2 "))
print(“Learning Python ”,end=““)
num3 = num1 + num2
print(“ Developed by Guido Van Rossum”)
print("Result =",num3)
Output
Learning Python  Developed by Guido Van Rossum
Save and execute by F5 and observe the result

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• WAP to calculate perimeter of rectangle


• Data type in Python specifies the type of data we are going to store in any
• WAP to enter radius and calculate area of circle variable, the amount of memory it will take and type of operation we can
• WAP to enter Name, marks of 5 subject and calculate total & perform on a variable. Data can be of many types e.g. character, integer,
percentage of student real, string etc.
• Python supports following data types:
• WAP to enter distance in feet and convert it into inches
 Numbers ( int, float, complex)
• WAP to enter value of temperature in Fahrenheit and convert it
 String
into Celsius.
 List
• WAP to enter radius and height of cylinder and calculate  Tuple
volume of cylinder.  Dictionary

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• From the name it is very clear the Number data types are used to • Integers allows to store whole numbers only and there is no fraction
store numeric values. Numbers in Python can be of following types: parts. Integers can be positive and negative e.g. 100, 250, -12, +50
(i) Integers • There are two integers in Python:
a) Integers(signed) 1) Integers(signed) : it is normal integer representation of whole
numbers. Integers in python can be on any length, it is only limited
b) Booleans
by memory available. In Python 3.x int data type can be used to
(ii) Floating point numbers store big or small integer value whether it is +ve or –ve.
(iii) Complex Numbers 2) Booleans: it allows to store only two values True and False. The
internal value of boolean value True and False is 1 and 0 resp. We
can get boolean value from 0 and 1 using bool() function.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Floating point number are mainly used for storing values like • Python represent complex numbers in the form A+Bj. To represent
distance, area, temperature etc. which have a fractional part. imaginary numbers, Python uses j or J in place of i. So in Python
• Floating point numbers have two advantage over integers: j = −1. Both real and imaginary parts are of type float
 they can represent values between the integers e.g.
 they can represent a much greater range of values a = 0 + 6j
• But floating point numbers suffers from one disadvantage also: b = 2.5 + 3J
 Floating point operations are usually slower than integer >>>a=4+5j
operations.
>>>print(a) # (4+5j)
• In Python floating point numbers represent machine level >>>b=0+2j
double precision floating point numbers i.e. 15 digit precision. >>>b #(2j)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Python allows to retrieve real and imaginary part of complex number • All string in Python is a sequence of Unicode characters. Unicode
using attributes: real and imag supports every characters from every language.
• If the complex number is a then we can write a.real or a.imag • Following are all legal strings in Python
• Example • “LEARNING”, “135”, “$$##”, “भारत”,

• >>>a=1+3.54j
• >>>print(a.real) # 1.0 • Every character in String is at a particular position called INDEX,
which starts from 0 (zero) i.e. first character will be at INDEX 0 and
• >>>print(a.imag) # 3.54 so on..

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• 1. A list in python represents a list of comma-separated values of any


• In Python string is a sequence of characters and each character can be individually
data type between square brackets
access using index. From beginning the first character in String is at index 0 and last
will be at len-1. From backward direction last character will be at index -1 and first • 2. It is mutable i.e. values are changeable
character will be at –len. • [10,20,30,40,50]
Forward indexing • [„a‟,‟e‟,‟o‟,‟i‟,‟u‟]
message 0 1 2 3 4 5 6
• [“KV”,208004,97.5]
W E L C O M E
• Example :
-7 -6 -5 -4 -3 -2 -1
• >>> family=["Mom","Dad","Sis","Bro"]
Backward indexing
• >>> print(family)
• ['Mom', 'Dad', 'Sis', 'Bro']
• >>> „Tommy‟ in family # Output will be false
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

 Dictionary is another feature of Python. It is an unordered set of comma separated


key:value pairs. Dictionary Items are defined in Curly Brackets { }
• Tuples as those list which cannot be changed i.e. not modifiable.  Keys defined in Dictionary cannot be same i.e. no two keys can be same.
Tuples are defined inside parenthesis and values separated by
comma. • >>> student={'Roll':1,'Name':"Jagga",'Per':91.5}
• >>> favorites=("Blue","Cricket","Gajar Ka Halwa") • >>>print(student)
• >>> print(student['Per'])
• >>> student=(1,"Aman",97.5)
• 91.5
• >>> print(favorites) • >>> val={1:100,2:300,4:900} # Key name can be string / numeric
• ('Blue', 'Cricket', 'Gajar Ka Halwa') • >>> print(val[1])
• >>> print(student) • 100
• Dictionary is mutable. i.e. We can modify dictionary elements.
• (1, 'Aman', 97.5) • >>>val[2]=1000
• >>>print(val) # {1: 100, 2: 1000, 4: 900}

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• Python data object can be broadly categorized into two types – mutable and
immutable types. In simple words changeable/modifiable and non-modifiable
types.
Core Data types
• 1. Immutable types: are those that can never change their value in place. In
python following types are immutable: integers, float, Boolean, strings, tuples
Numbers None Sequences Mappings
• Sample Code:
a = 10
Integers Floating Complex String Tuple List Dictionary
point b=a
c = 15 # will give output 10,10,30
From this code, you can say the value of integer a, b,c
Boolean a = 20 could be changed effortlessly, but this is not the case. Let
b = 40 us understand what was done behind the scene
c=b

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

a = 20
• Note: In python each value in memory is assigned a memory address. So each
time a new variable is pointing to that value they will be assigned the same
Now let us understand the changes done to variable a, b,c b = 40
address and no new memory allocation. Let us understand the case. c=b

value
10 15 20 21 40 55 value
10 15 20 21 40 55
address 250 272 280 284 290 312 address 250 272 280 284 290 312
>>> a=10 >>> a=20
a = 10 >>> b=a
b=a a >>> b=40
>>> c=15
c = 15 c b >>> c=b
a b c >>> print(id(a))
1757402304 >>> print(id(a))
>>> print(id(b)) 1757402464
Python provides id() function to get the >>> print(id(b))
Python provides id() function to get the 1757402304
>>> print(id(c)) memory address to which value /variable is 1757402784
memory address to which value /variable is
1757402384 referring >>> print(id(c))
referring
>>> print(id(10)) 1757402784
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & 1757402304 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• From the previous code it is clear that variable names are stored references to a • Mutable means in same memory address, new value can be
value-object. Each time we change the value the variable‟s reference memory stored as and when it is required. Python provides following
address changes. So it will not store new value in same memory location that‟s
mutable types:
why Integer, float, Booleans, strings and tuples are immutable.
• Variables (of certain type) are NOT LIKE storage containers i.e. with fixed memory 1. Lists
address where value changes every time. Hence they are immutable 2. Dictionaries
See, even if we
3. Sets change the value, its
reference memory
•Examples: (using List) address has
>>> employee=["E001","Rama","Sales",67000] remained same
>>> print(id(employee))
71593896
>>> employee[3]=75000
>>> print(id(employee))
71593896
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
>>> SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

type of an object determines the operations that can be performed


• Python is an object oriented language. So every thing in python is an on the object. Built – in function type() returns the type of an
object. An object is any identifiable entity that have some object
characteristics/properties and behavior. Like integer values are object Example:
– they hold whole numbers only(characteristics) and they support all >>> a=100
arithmetic operations (behavior).
>>> type(a)
• Every python object has three key attributes associated with it: <class 'int'>
>>> type(100)
1. type of object
<class 'int'>
2. value of an object
>>> name="Jaques"
3. id of an object
>>> type(name)
<class 'str'>
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com for more updates visit: www.python4csip.com

• While evaluating logical expressions, Python follows these rules: An explicit type conversion is user-defined conversion that forces an
• (i) the precedence of logical operators is lower than arithmetic expression to be of specific type. The explicit type conversion is also
operators. For e.g. known as Type Casting.
10/5 or 5.0 + 50/10 will be evaluated as 5 or 10.0 It is done using the syntax : datatype_to_convert(expression)
• The precedence of logical operators among themselves are For example:
NOT , AND , OR. So, str=„100‟ # String type
• (x and y or z and (not q)) will be evaluated as – Num = int(str) # will convert the str to int type
((x and y) or (z and (not q)))
• PYTHON USES SHORT-CIRCUIT CONCEPT WHILE EVALUATING
LOGICAL EXPRESSION

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

for more updates visit: www.python4csip.com

• import math # to include math library


• Important functions:
ceil(), sqrt(), exp(), fabs(), floor(), log(), log10(),
pow(), sin(), cos(), tan()

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

You might also like