Thursday, 11 June 2020

Python Datatypes




1+2
Out[1]:
3. . .


print("Hello, Welcome ")
Hello, Welcome
. . .

a=10
. . .

print (a)
10

a = 10
A = 10
if a == A :
    print ("True")
else:
    print("False")
True
. . .

a = 10
A = 10
if a is A :
    print ("True")
else:
    print("False")
True
. . .

a = 10
A = 20
if a == A :
    print("TRue")
else:
    print("False")
False
. . .

10/3
Out[8]:
3.3333333333333335
. . .

10//3
Out[9]:
3
. . .

10%3
Out[10]:
1
. . .
 a=10
a =+ 20
print(a)
20. . .

a+=30. . .

print(a)
50
. . .

x=10
. . .
 x is 10
Out[15]:
True
. . .

pi=3.16
print(type(pi))
<class 'float'>
. . .

str1="Hello"
print(str1)

str1="Hello"
print(str1)
Hello
. . .

str1="Hello"
print(str1[0])
print(str1[-1])
print(str1[::-1])
H
o
olleH
. . .

# Slicing [start : stop : interval]
s = "PythonIsLove"
res1 = s[-4:-10:-1]
res2 = s[-4:-10:-2]
res3 = s[-4:-10:-3]
print(res1)
print(res2)
print(res3)
LsInoh
LIo
Ln
. . .

a = '1_111'
print(type(a))
print(a)
print(float(a))
<class 'str'>
1_111
1111.0 


# Strings and its Function
. . .
  
str2=" my name is Kuldip "
str3=' '
str4 = "Singh"
str5=str2+str3+str4
print(str5)
print(str5.capitalize())
print(str5.count('i'))
print(str5.upper())
print(str5.lower())
print(str5.replace('i','*'))
print(len(str2))
print(len(str2.strip()))
str6 = "My Name Is Kuldip Singh"
print(str6.split())
print(type(str6))
print(type(str6.split()))
str7='''My Name is Kuldip.
I am from Odisa.
I live in Karnataka.'''
print(str7)
print(str7.find('My'))
print(str7.find('Kul'))
 my name is Kuldip  Singh
 my name is kuldip  singh
3
 MY NAME IS KULDIP  SINGH
 my name is kuldip  singh
 my name *s Kuld*p  S*ngh
19
17
['My', 'Name', 'Is', 'Kuldip', 'Singh']
<class 'str'>
<class 'list'>
My Name is Kuldip.
I am from Odisa.
I live in Karnataka.
0
11
. . .



# Tuple
. . .

t1=('a','b','c','d')
t2=('e','f','g')
print(type(t1))
print(t1+t2)
print(t1*2)
print(t1[2])
print(t1[1:3])
<class 'tuple'>
('a', 'b', 'c', 'd', 'e', 'f', 'g')
('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd')
c
('b', 'c')
. . .

# List
. . .

l1= [1123,'abc',11.34]
print(type(l1))
print (l1)
l1+=['Python',686,3.14]
print (l1)
#l1.remove[1] -- Error due to Immutable
print(l1)
print(l1[1:4])
l1.append('Thanks')
print(l1)
l2=['Kuldip','How are u ?']
l1.append(l2)
print(l1)
l1.insert(2,'number')
print(l1)
l1.insert(8,'Hey!!')
print(l1)
l1.insert(9,'Hi!!')
print(l1)
l1.insert(12,'Hello!!')
print(l1)
<class 'list'>
[1123, 'abc', 11.34]
[1123, 'abc', 11.34, 'Python', 686, 3.14]
[1123, 'abc', 11.34, 'Python', 686, 3.14]
['abc', 11.34, 'Python']
[1123, 'abc', 11.34, 'Python', 686, 3.14, 'Thanks']
[1123, 'abc', 11.34, 'Python', 686, 3.14, 'Thanks', ['Kuldip', 'How are u ?']]
[1123, 'abc', 'number', 11.34, 'Python', 686, 3.14, 'Thanks', ['Kuldip', 'How are u ?']]
[1123, 'abc', 'number', 11.34, 'Python', 686, 3.14, 'Thanks', 'Hey!!', ['Kuldip', 'How are u ?']]
[1123, 'abc', 'number', 11.34, 'Python', 686, 3.14, 'Thanks', 'Hey!!', 'Hi!!', ['Kuldip', 'How are u ?']]
[1123, 'abc', 'number', 11.34, 'Python', 686, 3.14, 'Thanks', 'Hey!!', 'Hi!!', ['Kuldip', 'How are u ?'], 'Hello!!']
. . .

# Dictionary
. . .

d1 = {}
print(type(d1))
print(d1)
d1={1:'123',2:'Kuldip'}
print(d1)
d1={1:'123',2:'Kuldip',3:1.4}
print(d1)
print(d1[1])
#print(d1[5]) -- Error as no key numbered 5
print(d1[3])
print(len(d1))
print(d1.values())
print(d1.keys())
<class 'dict'>
{}
{1: '123', 2: 'Kuldip'}
{1: '123', 2: 'Kuldip', 3: 1.4}
123
1.4
3
dict_values(['123', 'Kuldip', 1.4])
dict_keys([1, 2, 3])
. . .

# Sets
. . .

myset1 ={}
print(type(myset1))
print(myset1)
myset1 ={1,2,3,4}
print(myset1)
myset2 ={3,4,5,6}
print(myset2)
print(myset1 & myset2) # intersection
print(myset1 | myset2) # Union
print(myset1 - myset2) # Difference
<class 'dict'>
{}
{1, 2, 3, 4}
{3, 4, 5, 6}
{3, 4}
{1, 2, 3, 4, 5, 6}
{1, 2}
. . .

# Flow control
. . .

Fruits = ['Apple', 'Banana','Grapes','Orange','Guava']
for x in Fruits:
    print(x)
Apple
Banana
Grapes
Orange
Guava
. . .

Fruits = ['Apple', 'Banana','Grapes','Orange','Guava']
if Fruits[2]=='Grapes':
    print ('Available')
else:
    print ("Not available")
Available
. . .

a=1
b=1
while a<5:
    while b <= a+1:
        print(b)
        b=b+1
    a=a+1
1
2
3
4
5
. . .

rows = 5
for num in range(rows):
    for i in range(num):
        print(num, end=" ")  # print number
    # line after each row to display pattern correctly
    print(" ")

2 2 
3 3 3 
4 4 4 4 
. . .
 x
rows = 10
for num in range(1, rows+1):
    for i in range(1, num + 1):
        print(i, end=' ')
    print("")
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
. . .
 

No comments: