Everything know about the Python
Python is a general purpose and high level programming language which introduced by Guido van Rossum in 1991.
It is used for :
Features :
print() function :
a built-in function which used for print anything.
syntax : print(value, .... , sep = ' ', end='/n', file= sys.stdout, flush=False)
print('Hello World') #Hello World
print('Hello','World',sep='-') #Hello-World
#bydefault end='\n'
print('Hello', ) #Hello
print('World') #World
#if changed then,
print('Hello', end=' ' )
print('World') #Hello World
Data Types
#Basic Data Types
a = 10 #int
b = 2.5 #float
c = True #boolean
c' = False #boolean
d = 'LinkedIn' #str (string)
e = 3+4j #complex
# Container Based
a = [1,2,3,'Hi'] #List
b = (1,2,3,'python') #tuple
c = {'name' : 'raj', 'age': 21} #dict
d = { 1,2,3, 'ok'} #set
#User-Defined
class Myclass:
print('This is my class')
Type Conversion
Type Conversion is used for convert a datatype from another.
for example, if you want convert int to float then :
a = int(2.3) #2
b = float(4) #4.0
c = int('10') #10
Keywords
Keywords are a reserved word which has some special meanings.
For Example - as, if , elif, break , continue etc.
Python has 33 keywords.
import keyword
print(keyword.kwlist) #show all keyword list
Variables
Variable - like a container which used to store values
for example, a = 3
Rules For Assign Variable
Literal : Literals are the values are assigned in variable from different formats.
for example ,
a = '0b1010' #binary
a = '0x12c' #hexa
a = '0o312' #octa
a = 1.5e10 #float
a = 'python'
a ="python"
a = ''' python '''
a = u'\uf1243\ugh6784\' #emoji
a = r' a \n b'
a = True + 4 #5
b = False + 10 #10
a = None #nothing in a ( used for variable declaration)
Operators
# Arithmetic Operators ( + , - , * , / , ** , % , // )
print(3+4) # 7
print(3-4) #-1
print(3*4) #12
print(3/4) #.75
print(3**4) #81
print(21//4) #1
print(21%4) #5
#Relational Operators ( > , < ,>=, <= , ==, !=)
print(5>3) #true
print(5<3) #false
print(5>=3) #true
print(5<=3) #false
print(5==3) #false
print(5!=3) #true
#Logical Operators ( and, or , not)
a = True
b = False
print(a and b) #False
print(a or b) # true
print(not a) #false
#Bitwise Operators ( & , | , ~ , <<, >>)
#bitwise and
print( 3 & 5) # 011 and 101 = 001 # --- (1)
#bitwise or
print( 3 | 5) # 011 or 101 = 111 # --- (7)'
#bitwise left shift
print(3<<5) # 3*(2^5) = 32
# bitwise right shift
print(12>>2) #12/(2^2) = 3
# bitwise not
print(~3) #-3
#Identity Operators ( is , is not)
a = 'Hello'
b = 'world'
print( a is b) #true
print(a is not b) #false
#Membership Operators ( in, not in)
print('D' in 'Delhi') #true
print('d' not in 'Delhi') #true
#Assignments Operators ( =, +=,-=,*=, /=)
a = 3
a+ = 3
print(a) #6
a *= 3
print(a) #18
a /= 3
print(a) #3
Input Function
Input function is used for take input from user.
for example , name = input('enater your name);
age = int(input('enter your age'))
if-else statements
if-else staements used for conditional logic building for example if condition true that's doing this else condition false then do this.
# Login System if email=='datatrick@gmail.com' and password=='py123': print('welcome') elif email=='datatrick@gfmail.com' and password!='py123': print('Incorrect Password') password = input('Enter Correct password') if password == 'py123': print('welcome') else: print('still incorrect') elif email != 'datatrick@gfmail.com' and password =='py123': print('Incorrect Email') else: print('Incorrct Password or Email')
Loops
Loops are used for iteration and repeat your task again and again until your condition is not false.
There have two types of loops :
While Loop
while i<11:
print(i)
i+=1
# Output
1
2
3
4
5
6
7
8
9
10
For Loop
For Loop iterate for range function and sequnce data like string or list .
for i in arnge(1,11):
print(i)
1
2
3
4
5
6
7
8
9
10
for i in ([1,,2,3,4]):
print(i)
1
2
3
4
Break, Continue and Pass Statement
Break Statement is used for terminate loop for specified condition.
Continue statment is used for skip the loop for specified condition.
Pass statement is used for not doing (empty loop) anything in loop , function or classes.
# break
for i in range(1,11):
if i==5:
break
print(i)
1
2
3
4
5
# continue
for i in range(1,11):
if i==5:
continue
print(i)
1
2
3
4
6
7
8
9
10
for i in range(1,11):
pass
Built-In Function in Python
Built-in Modules
String
String is asequence of characters.
For Example , "python is popular language" is string.
create string
a = 'python is object-oriented'
a = "python is high level"
a = '''johny,johy,yes papa,
Eating Sugar, No Papa !''' #multi-line string
a = str('python learn easy')
accesing substring from string
a = 'python is best'
#first char
a[0] #p
#last char
a[-1] #t
#substring
a[0:6] #python
a[0:8:2] #ptoi
Editing and deleting string
Editing in String is not possible because string are immutable ( not changeable)
Deleting -
a = 'python is best'
del a # delete string
Recommended by LinkedIn
but , if you do this
del a[-1] #this not possible
String Operation
a = 'Hello'
b = 'World'
a + b # HelloWorld
a * 3 # HelloWorldHelloWorldHelloWorld
'H' in a #true
'h not in a #true
'Hello'>'World' #false lexicography dict based ( which character after called big)
'Hello'<'World' #true
We know that - ' ' ---->False ; 'python' ---->True
'Hello' and 'World' #World
'Hello' or 'World' #Hello
Functions in String
Python Data Structures
List - List is a mutable data type which used to store heterogeneous values.
#Create a list
a = [1,2,3,'hi']
a = list(1,2,3,'hi')
a = [] #empty list
#update a list
a[0] = 100
a #[100,2,3,'hi']
#add value in list
#delete list
#list operations
l1 = [1,2,3]
l2 = [4,5,6]
l1 + l2 #[1,2,3,4,5,6]
l1 *3 #[1,2,3,1,2,3,1,2,3]
3 in l1 #yes
5 in l1 #no
#List function
Tuple : It is a immutable data type which used to store heterogeneous values.
#create tuple -
a = (1,2,3)
a = tuple(1,2,3)
a = (1,) #single element tuple
#edit tuple : it's not possible because it is immutable.
#delete tuple :
#Tuple Operations
t1 = (1,2,3)
t2 = (4,5,6)
t1 + t2 #(1,2,3,4,5,6)
t * 2 #(1,2,3,1,2,3)
2 in t1 #yes
8 in t2 #no
#Tuple functions
Sets
Rules for a set -
#create set
a = {1,2,3}
a = set(3,2,1)
a = {} #it doesn't create empty set set
a = set() #create empty empty set
#add a value in set
a.insert(5)
#Operation in set
s1 = {1,2,3}
s2 = {4,5,6}
s1 + s2 #{1,2,3,4,5,6}
#function
len(s1) #3
type(s1) #set
sorted(s1)
s1.union(s2) #{1,2,3,4,5,6}
s1.intersection(s2) #{}
Dict
Dictionary is a mutable data type which used to store value as key-value pairs.
#create dict
d1 = {'name' : 'raj', 'age' : 21}
d1 = dict(name' : 'raj', 'age' : 21)
d1 ={} #empty dict
#access keys
d1.keys()
#access values
d1.values()
#using for loop
for i in d1:
print(i,d[i])
#add a value in dict -
d1['gender']='male'
#update value in dict
d1['name'] = 'raj singh'
#delete
del d1['gender']
del d1 #whole dict delete
d1.clear() #{}
d1.pop() #last key-value