Professional Documents
Culture Documents
Placement Python 2
Placement Python 2
Features of python
d=1.10 y=0.123
print(d) print(y)
print(type(d)) print(type(y))
z= -0.123
print(z)
print(type(z))
complex numbers:Complex numbers in python are in the form
of a + bj
x=3+5j
print(x)
z=-5j
print(type(x)) y=5j print(z)
print(y) print(type(z))
print(type(y))
(3+5j)
<class ‘complex’>
Python List
Python tuple
L=[1,2,3]
print(L) T=(1,2,3)
print(type(L)) print(T)
print(type(T))
Output:
Output:
[1,2,3]
<class ’list’>
(1,2,3)
<class ’tuple’>
Python Set Python dictionary
a={1,2,3,4} d = {1:'v1','2':’v2’}
print(a) print(d)
print(type(a)) print(type(d))
{1,2,3,4} {1:'v1','2':’v2’}
<class ’set’> <class ’dict’>
Dictionary
Elements are represented as pair of key and value
Dictionaryname={key1:value1,key2:value2…………….keyn:valuen}
range(2,6) 2,3,4,5
range(1,4) 1,2,3
range(start,end,step) range(-5,0,1)
range(1,10,2) 1,3,5,7,9
-5,-4,-3,-2,-1
range(2,12,2) 2,4,6,8,10
for i in range(0,3): #print even numbers
print(i) for i in range(0,11,2):
print(i)
for i in range(1,3):
print(i) Looping Through a String
Syntax:
pass
for x in [0, 1, 2]:
pass
def myfunction:
pass
LIST
List is collection of ordered elements,surrounded by square brackets[].
List is mutable(changeable)
List is dynamic. They grow and shrink on demand.
The list is better for performing operations, such as insertion and deletion.
Lists consume more memory
Lists have several built-in methods
l=[]
l1=[1,2,3]
l2=[‘p’,’r’,’o’,’b’,’e’]
#List with mixed datatypes
a=[1,”hai”,3.4]
print(a)
slicing: -7 -6 -5 -4 -3 -2 -1
t = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’] t = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’]
print(t) 0 1 2 3 4 5 6
print(t[1])
print(t[1:3]) ['a', 'b', 'c', 'd', 'e', 'f', 'g']
b
print(t[1:-1]) ['b', 'c']
['b', 'c', 'd', 'e', 'f']
print(t[:3]) ['a', 'b', 'c']
['c', 'd', 'e', 'f', 'g']
print(t[2:]) ['a', 'b', 'c', 'd', 'e', 'f']
['b', 'd', 'f']
print(t[:-1]) ['g', 'f', 'e', 'd', 'c', 'b', 'a']
print(t[1::2])
print(t[::-1])
#Removing duplicates in a list
my_list = [1,1,2,3,2,2,4,5,6,2,1]
my_final_list = set(my_list)
print(list(my_final_list))
Output:
[1, 2, 3, 4, 5, 6]
Comprehensions in Python provide us with a short and concise way to construct new sequences
(such as lists, set, dictionary etc.) using sequences which have been already defined.
Represented by dict() or {}
Keys in dictionary are unique and they are immutable(strings,numbers,tuples)
Let’s consider the following example which demonstrates function definition and function call:
#Write a program to calculate cube of a number
def cube(x): 2
“”” calculating cube without using return and different
name for formal and actual values”””
print(x*x*x) 8
a=int(input(“Enter the value of a:”)) 2
cube(a)
#Write a program to calculate cube of a number using
return
def cube(x): 2
“”” calculating cube with using return”””
return(x*x*x)
x=int(input(“Enter the value of x:”)) 2
y=cube(x) 8
print(“Cube of the number”,y)
8
Recursive Function:
Accessible throughout the program file. Accessible from the point it is defined to the end of
the block it is defined in.
Accessible to all functions in the program. They are not related in any way to other variables
outside the function.
x = 20
def fun():
x = 10
print("Local x =",x)
fun()
print("Global x =",x)
Local x = 10
Global x = 20
Anonymous Functions or LAMBDA Functions
Modular programming is a way of organizing programs as they become more
complicated. we can create a Python module, a source file that contains Python source
code to do something useful, and then import this module into our program so that you
can use it.
Creation of Module:
mymodule.py
def greeting(name): Output:Hello,CRT
print("Hello, " + name)
Use of Module:
import mymodule
mymodule.greeting("CRT")
__init__() Method (Constructor)