Copy of Python Revision PPT
Copy of Python Revision PPT
Revision Tour
Mr. Nikhil Sharma
P.G.T (Computer Science )
DSPS-7, Karnal
Introduction
Pros of Python :
● Easy to use – Due to simple syntax rule
● Interpreted language – Code execution & interpretation line by line
● Cross-platform language – It can run on windows, Linux, Macintosh etc. equally
● Expressive language – Less code to be written as it itself express the purpose of the code.
● Completeness – Support wide rage of library
● Free & Open Source – Can be downloaded freely and source code can be modify for improvement
Cons of Python
● Lesser libraries – as compared to other programming languages like C++, java, .NET
● Slow language – as it is interpreted languages, it executes the program slowly.
● Weak on Type-binding – It not pin point on use of a single variable for different data type
Data types in Python
● We know that data types are nothing but the type of data we use to the variable,
method etc.
● We have following data types in Python.
1. Number
2. String
3. Boolean
4. List
5. Tuple
6. Set
7. Dictionary
Number
In Python we use number data type to store numeric value.
● Integer – Whole number without fraction parts ex: 1, 2 etc. Use 32 bits to store
value.
● Long integer – It store larger integer number. Use 32 bits to store value.
● Floating Point – It is a positive or negative real numbers with a
decimal point.
● Complex – Complex numbers are combination of a real and imaginary part.
Complex numbers are in the form of X+Yj, where X is a real part and
Y is imaginary part.
Input : a = complex(5) Output : (5+0j)
print(a)
String
● A string is a sequence of characters. In python we can create string using single (' ') or double quotes (" ").Both are
same in python.
Ex : - Input :
str='computer science’
print('str-', str) # print string
Output :
str- computer science
String Method
Method Description
Capitalize() Returns a copy of string with its first character as capita
Find() Returns the lowest index in the string
Isalnum() Returns true if the character in the string are alphanumeric
Isalpha() Returns true if the character in the string are alphabetic
Isdigit() Returns true if the character in the string are digit
Islower() Returns true if all the cased characters in the string are lowercase
Isupper() Returns true if all the cased characters in the string are uppercase
Title() Returns True if the string is in title case
Swapcase() Returns a copy of the string with uppercase character converted to lowercase and
vice versa
Partition() Splits the string at the first occurrence of argument
String Methods contd..
Method Description
Isspace() Returns true if there are only whitespace character in the string
Count() Returns the number of non-overlapping occurrence of substring in the given string
lstrip Returns a copy of the string with leading characters removed
rstrip Returns a copy of the string with trailing characters removed
Startswith() Returns true if string starts with the argument otherwise return false
Endswith() Returns true if string ends with the argument otherwise return false
LIST
List are collections of items and each item has its own index value.
List Methods
Method Description
Index() Returns the index of first matched item from the list
Append() Adds an item to the end of the list
Extend() Adds list at the end
Insert() Insert any item in between the list
Pop() Remove an item from the list
Remove() Remove the first occurrence of the given element
Clear() Remove all the items from the list
Count() Returns the count of the item that passes as argument
Reverse() Reverses the item of the list
Sort() Sort the item of the list, by default in increasing order
Tuples
● Tuples are list of value separated by comma ( , ).
● Tuples are Immutable which means values in the tuple can not be changed.
● The data type in tuple can be any. Which means we can have number and string in same tuple.
● Tuple is represented as : ( )
Example: 1) t1 = (1,2,3,4,5,6)
2) t2 = (‘A’ , ‘B’ , ‘C’)
3) t3 = (‘a’ , ‘b’ , 1,2,3,4,5,6)
Tuple Methods
Method Description
Len() Return the length of tuple
Max() Returns the element from the tuple having maximum value
Min() Returns the element from the tuple having minimum value
Index() Returns the index of an existing element of a tuple
Count() Returns the count of the member element / object in a given sequence(
list/tuple)
Tuple() Constructor method, create tuple
Dictionary
● It is an unordered collection of items and each item consist of a key and a value.
e.g. dict = {'Subject': 'comp sc', 'class': '11’}
print(dict)
print ("Subject : ", dict['Subject’])
print ("class : ", dict.get('class’))
Output
{'Subject': 'comp sc', 'class': '11’}
Subject : comp sc class : 11
class : 11
Dictionary Method
Method Description
len() Returns length of the dictionary
clear() Remove all method from the dictionary
get() Get the item with the given key
has_key To check the presence of given key in the dictionary
s
items() Return all the items of the dictionary
key() Return all the keys in the dictionary
value() Returns all the values from the dictionary in the form of list in no particular order
update() This will merge the key : value pairs from a new dictionary, adding or replacing as
needed
cmp Compare to dictionary based on their elements
List
● A list in python is represented by square bracket [].
● List is assigned to Variable
● The value inside a list can be changed.
example : (1) a = [1,2,3,4,5]
(2) b = [‘A’ , ‘B’ , ‘C’]
(3) c = [1,2, ’A’ , ‘B’ ]
Mutable and Immutable
Mutable data type can change Immutable data type can’t be
the value change value
● Dictionary ● String
● List ● Integer
● Tuples
● Booleans
● Floating Point
Arithmetic Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator
operates on is called the operand.
Arithmetic operators Used for mathematical operation
Operator Meaning Example
+ Add two operands x + y +2
- Subtract right operand from the left x - y -2
* Multiply two operands x*y
/ Divide left operand by the right one x/y
% Modulus - remainder of the division x%y
// Floor division - division that results into whole number x // y
** Exponent - left operand raised to the power of right x**y
Comparison operators - Used to compare values
Operator Meaning Example
Input Output
x=1 Matched
y=2
if(x==1 and y==2):
print(‘Matched')
if-else Statements
● If-else statement executes some code if the test expression is true
and some other code if the test expression is false.
Input Output
a=10 less than 100
if(a < 100):
print(‘less than 100')
else:
print(‘more than equal
100')
Nested if-else statement
The nested if...else statement allows you to check for multiple test expressions and execute different codes for more
than two conditions.
Input Output
Input Output
for letter in ‘rajeev': r
if letter == 'e' or letter == ‘a': J
continue v
print( letter)
Break Statement: It brings control out of the loop.
Input Output
for letter in 'rajeev': Current Letter : a
Pass Statement: We==
if letter use'a'pass statement
or letter == 'j': to write empty loops. Pass is also used for empty control statements,
function and classes. break
print('Current Letter :', letter)