Python Programming Examples
Python Programming Examples
creative thchnology
Assignment No-01- Starting To Mid
Course Title-Python Programming Language
Course Code-CSE4116
Submitted To
Dr. Md. Robiul Islam
Lecturer of SMUCT
Department of CSE & CSIT
Submitted BY
Name : Md Hasan Miah
Id : 183071021
Semester : 10th
Department : CSE
1
June 8, 2022
print("hello world")
hello world
x= 5
y = "Hasan"
z = 20.05
print(type(x))
print(type(y))
print(type(z))
<class 'int'>
<class 'str'>
<class 'float'>
2
#String are Array
c = "Hello World"
print(a[1])
#String length
d = "hellow world"
print(len(d))
md hasan miah
my name is md hasan maih i am from comilla
d
12
True
No, 'expensive' is NOT present.
[12]:
llo
b = "Hello, World!"
print(b[2:5])
b = "Hello, World!"
print(b[:5])
c = "Hello, World!"
3
print(c[2:])
#negative index
d = "hellow world"
print(d[-5:-2])
llo
Hello
llo, World!
wor
a = "this is an Apple"
print(a.upper())
#remove whitespace
c = "hello, world"
print(c.strip())
#replacing String
d = "hello world"
print(d.replace("h","j"))
THIS IS AN APPLE
my name is hasan
hello, world
jello world
a = "Hello"
b = "World"
c=a+b
print(c)
4
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
HelloWorld
I want 3 pieces of item 567 for 49.95 dollars.
We are the so-called "Vikings" from the north.
if 5 > 2:
print("Five is greater than two!")
else:
print("Five is less than two!")
#CREATING VARIABLE
#casting
#get the type
x = str(5)
y = int(6)
z = float(4)
a = "hasan"
print(type(x))
print(type(y))
print(type(z))
print(type(a))
b,c,d = "md","hasan","miah"
print(b)
5
print(c)
print(d)
e=f=g="hasan"
print(e)
print(f)
print(g)
#unpack a colllection
name = ["md","hasan","miah"]
h=i=j=name
print(h)
print(i)
print(j)
k = "my "
l = "is "
m = "Hasan"
print(k+l+m)
#GLOBAL VARIABLE
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
<class 'str'>
<class 'int'>
<class 'float'>
<class 'str'>
md
hasan
miah
hasan
hasan
hasan
['md', 'hasan', 'miah']
['md', 'hasan', 'miah']
6
['md', 'hasan', 'miah']
my is Hasan
Python is fantastic
#Integers:
x = int(1)
y = int(2.8)
z = int("3")
print(x)
print(y)
print(z)
#Floats:
a = float(1)
b = float(2.8)
c = float("3")
d = float("4.2")
print(a)
print(b)
print(c)
print(d)
#Strings:
e = str("s1")
f = str(2)
g = str(3.0)
print(e)
print(f)
print(g)
1
2
3
1.0
2.8
3.0
4.2
s1
2
3.0
7
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
class myclass():
def len (self):
return 0
myobj = myclass()
print(bool(myobj))
def myFunction() :
return True
if myFunction():
print("YES!")
else:
print("NO!")
a= 5
b = 10
c = 15
d =20
e = a+b
f = d-c
g = d/a
h = a*b
print(e)
print(f)
print(g)
print(h)
print(a+b-c*d/f)
15
5
8
4.0
50
-45.0
mylist = ["arum","bean","arum","brinjal"]
print(mylist)
print(len(mylist))
print(type(mylist))
print(thislist)
print(type(thislist))
9
fruits_item = ["apple", "banana", "cherry"]
fruits_item[1] = "blackcurrant"
print(fruits_item)
fruits_item[1:2] = ["mango","watermilon"]
print(fruits_item)
fruits_item.insert(2,"orange")
print(fruits_item)
10
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i =i + 1
#Sort Descending
#revers order
11
['arum', 'bean', 'arum', 'brinjal']
4
<class 'list'>
['apple', 'banana', 'cherry']
[1, 5, 7, 9, 3]
[True, False, False] ['apple',
'banana', 'cherry']
<class 'list'>
banana
banana
['cherry']
['apple', 'banana', 'cherry']
['cherry']
['cherry']
['apple']
Yes, 'apple' is in the fruits list
['apple', 'blackcurrant', 'cherry']
['apple', 'mango', 'watermilon', 'cherry']
['apple', 'mango', 'orange', 'watermilon', 'cherry']
['apple', 'cherry']
[]
apple
banana
cherry
apple
banana
cherry
apple
banana
cherry
['apple', 'banana', 'mango']
['banana', 'kiwi', 'mango', 'orange', 'pineapple']
['pineapple', 'orange', 'mango', 'kiwi', 'banana']
['cherry', 'Kiwi', 'Orange', 'banana']
['apple', 'banana', 'cherry']
# Empty tuple
my_tuple = ()
print(my_tuple)
12
# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
print(a)
print(b)
print(c)
my_tuple = ("hello")
print(type(my_tuple))
# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))
print(my_tuple[0])
print(my_tuple[5])
# nested tuple
13
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
# nested index
print(n_tuple[0][3])
print(n_tuple[1][1])
print(my_tuple[-1])
print(my_tuple[-6])
print(my_tuple[1:4])
print(my_tuple[:-7])
print(my_tuple[7:])
print(my_tuple[:])
14
# Tuples can be reassigned
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)
# Concatenation
# Repeat
print(("Repeat",) * 3)
# Tuple method
print(my_tuple.count('p'))
print(my_tuple.index('l'))
# In operation
print('a' in my_tuple)
print('b' in my_tuple)
# Not in operation
print('g' not in my_tuple)
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
(3, 4.6, 'dog')
3
4.6
dog
15
<class 'str'>
<class 'tuple'>
<class 'tuple'>
p
t
s
4
t
p
('r', 'o', 'g')
('p', 'r')
('i', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
(4, 2, 3, [9, 5])
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
(1, 2, 3, 4, 5, 6)
('Repeat', 'Repeat', 'Repeat')
2
3
True
False
True
# initialize my_set
my_set = {1, 3}
print(my_set)
# add an element
my_set.add(2)
print(my_set)
16
# add multiple elements
my_set.update([2, 3, 4])
print(my_set)
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
my_set.discard(4)
print(my_set)
# remove an element
my_set.remove(6)
print(my_set)
my_set = set("HelloWorld")
print(my_set)
# pop an element
print(my_set.pop())
# clear my_set
my_set.clear()
print(my_set)
17
# use | operator
# Output: {1, 2, 3, 4, 5, 6, 7, 8}
print(A | B)
# Intersection of sets
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use - operator on A
print(A - B)
# in keyword in a set
# initialize my_set
my_set = set("apple")
{1, 2, 3}
{1.0, 'Hello', (1, 2, 3)}
{1, 2, 3}
{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
{1, 3, 4, 5, 6}
{1, 3, 5, 6}
{1, 3, 5}
{'l', 'W', 'r', 'd', 'o', 'e', 'H'}
l
{'r', 'd', 'o', 'e', 'H'}
set()
{1, 2, 3, 4, 5, 6, 7, 8}
{4, 5}
18
{1, 2, 3}
True
False
# initialize dict1
dict1 = {
"brand": "Ford",
"model": "Musting",
"year": 1964,
"year": 2000
}
print(dict1)
print(dict1["year"])
print(len(dict1))
dict1 = {
"brand": "Ford",
"model": "Musting",
"year": 1964,
"colors": ["red", "green", "blue"]
}
print(dict1)
print(dict1["colors"])
print(dict1["colors"][2])
dict1["colors"][2] = "yellow"
print(dict1)
print(type(dict1))
dict1["year"] = 2022
dict1.update({"year":2020})
dict1.pop("model")
print(dict1)
19
print("yes colors is one o the keys in dict1 dictionary")
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
thisdict1 = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict1:
print(x)
key_list = list(key)
print(type(key_list))
print(key_list[1])
for i in range(len(key_list)):
if key_list[i] == "model":
print("Model key Exists")
else:
continue
for x in key:
if x == "model":
20
print("Model key Exists")
else:
continue
myfamily = {
"child1":{
"name":"Rahim",
"year": 1889
},
"child2":{
"name":"tuli",
"year": 1989
},
"child3":{
"name": "karim",
"year": 2020
}
}
print(myfamily["child1"])
print(myfamily["child1"]["name"])
print("length of dictionary:")
print(len(myfamily))
print("Keys of dictionary:")
for x in myfamily.keys():
print(x)
print("Values of dictionary:")
for y in myfamily.values():
print(y)
dict2=dict1.copy()
21
print(dict2)
#clear() method
dict1.clear()
print(dict1)
#get() method
print(dict2.get(1))
#items() method
print(dict2.items())
#keys() method
print(dict2.keys())
#pop() method
dict2.pop(4)
print(dict2)
#popitem() method
dict2.popitem()
print(dict2)
#update() method
dict2.update({3:"Scala"})
print(dict2)
# values() method
print(dict2.values())
22
year
dict_keys(['brand', 'model', 'year'])
<class 'list'>
model
Model key Exists
Model key Exists
{'name': 'Rahim', 'year': 1889}
Rahim
length of dictionary:
3
Keys of dictionary:
child1
child2
child3
Values of dictionary:
{'name': 'Rahim', 'year': 1889}
{'name': 'tuli', 'year': 1989}
{'name': 'karim', 'year': 2020}
Index wise values of dictionary:
Rahim
tuli
karim
{1: 'Python', 2: 'Java', 3: 'Ruby', 4: 'Scala'}
{}
Python
dict_items([(1, 'Python'), (2, 'Java'), (3, 'Ruby'), (4, 'Scala')])
dict_keys([1, 2, 3, 4])
{1: 'Python', 2: 'Java', 3: 'Ruby'}
{1: 'Python', 2: 'Java'}
{1: 'Python', 2: 'Java', 3: 'Scala'}
dict_values(['Python', 'Java', 'Scala'])
[ ]:
23