Python Lab 1 - Jupyter Notebook
Python Lab 1 - Jupyter Notebook
In [ ]: 1 # aaaaaaaa
2 # bbbnnnn
3 # cccccc
hello world
Arithmetic Operations
In [2]: 1 # addition
2 # subtraction
3 # multiplication
4 # division
5 # exponentiation
6 # quotient
7 # modulas
In [3]: 1 10+2+3
Out[3]: 15
In [4]: 1 100+23+100
Out[4]: 223
In [5]: 1 10-3
Out[5]: 7
In [6]: 1 10-4-17
Out[6]: -11
In [7]: 1 12*5
Out[7]: 60
In [8]: 1 3.4*98
Out[8]: 333.2
In [9]: 1 18/2
Out[9]: 9.0
In [10]: 1 13/2
Out[10]: 6.5
In [11]: 1 pow(2,3)
Out[11]: 8
In [12]: 1 10/3
Out[12]: 3.3333333333333335
In [13]: 1 10//3
Out[13]: 3
In [14]: 1 40//7
Out[14]: 5
In [15]: 1 35.5//4
Out[15]: 8.0
In [16]: 1 10%3
Out[16]: 1
In [17]: 1 68%11
Out[17]: 2
Variables
In [18]: 1 var = 20
In [19]: 1 var
Out[19]: 20
In [20]: 1 x = 3
2 y = 5
In [23]: 1 x+y
Out[23]: 8
2.15
2.154
Data Types
In [ ]: 1
In [30]: 1 x = 10
2 y = "ritu"
3 z = 19.4
4 q = False
In [27]: 1 type(x)
Out[27]: int
In [28]: 1 type(y)
Out[28]: str
In [29]: 1 type(z)
Out[29]: float
In [31]: 1 type(q)
Out[31]: bool
Type Casting
In [32]: 1 x = '12'
In [33]: 1 type(x)
Out[33]: str
In [35]: 1 int(x)
Out[35]: 12
In [36]: 1 y = int(x)
In [37]: 1 type(y)
Out[37]: int
If-else
In [48]: 1 age = 15
2 if age>25:
3 print("You are an adult")
4 else:
5 print("You are an adult")
Loop
In [59]: 1 #while:
2 #code
3 #condition
4
5
6 i = 1
7 while i <10:
8 print("I love python")
9 i = i+1
10
I love python
I love python
I love python
I love python
I love python
I love python
I love python
I love python
I love python
In [106]: 1 i = 1
2 while i < 6:
3 print(i)
4 i += 1
even
34
even
21
odd
45
odd
odd
even
67
odd
78
even
76
even
54
even
apple
banana
cherry
apple
banana
In [ ]: 1 #Q.Write a Program to find the sum of all numbers stored in a list
Function
localhost:8888/notebooks/Python Lab 1.ipynb#If-else 5/14
6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook
Mina Zaman
Farhim Zaman
Tina Zaman
User Input
In [38]: 1 input()
10
Out[38]: '10'
Out[39]: 'ritu'
In [41]: 1 x
Out[41]: '10'
In [43]: 1 x
Out[43]: 10
Enter a number: 2
number 2: 4
In [52]: 1 print(a+b)
In [105]: 1 #Q Input a number from user and check whether the num is odd / even
2 #Q creats a program that takes the input of usee name and password. If the u
3 #print a welcome message else print try again.(define username & password fi
String
In [66]: 1 data = "I lovee data science"
In [67]: 1 type(data)
Out[67]: str
In [68]: 1 len(data)
Out[68]: 20
Out[71]: 69
In [72]: 1 data.count("data")
Out[72]: 1
Out[73]: 8
In [74]: 1 data.find('v')
Out[74]: 4
In [75]: 1 data.find("v",6,10)
Out[75]: -1
In [76]: 1 data.index('v')
Out[76]: 4
In [77]: 1 data.index("w")
---------------------------------------------------------------------------
<ipython-input-77-50a047b1f2db> in <module>
----> 1 data.index("w")
In [78]: 1 data.upper()
In [79]: 1 data.lower()
In [81]: 1 data.capitalize()
In [82]: 1 data.swapcase()
In [83]: 1 data.title()
In [87]: 1 data.split()[1]
Out[87]: 'lovee'
In [88]: 1 data.replace('data','ai')
In [139]: 1 #Write a Python program which accepts the user's first and last name and pri
2 #with a space between them.
3
4 fname = input("Input your First Name : ")
5 lname = input("Input your Last Name : ")
6 print ("Hello " + lname + " " + fname)
In [97]: 1 # list []
2 #a list of comma-separated values (items) between square brackets.
3 #Important thing about a list is that items in a list need not be of the sam
In [90]: 1 n = [1,2,3,"ritu",True,(1,2,3)]
In [91]: 1 n
In [92]: 1 n[0]
Out[92]: 1
In [104]: 1 n[0:4]
In [93]: 1 n[-1]
Out[93]: (1, 2, 3)
In [94]: 1 n[-1][0]
Out[94]: 1
In [143]: 1 #Write a Python program to display the first and last colors from the follow
2 #color_list = ["Red","Green","White" ,"Black"]
3
In [142]: 1 type(n)
Out[142]: list
1997
2001
Out[117]: [1, 2, 3, 4, 5, 6]
In [118]: 1 ['Hi!'] * 4
In [ ]: 1
In [ ]: 1
In [99]: 1 t = (1,2,3,"ritu",True,(1,2,3))
In [100]: 1 t
In [101]: 1 t[0]
Out[101]: 1
In [103]: 1 t[0:4]
tup1[0]: physics
tup2[1:5]: (2, 3, 4, 5)
In [123]: 1 #dictionary
2 #Each key is separated from its value by a colon (:)
3 # the items are separated by commas, and the whole thing is enclosed in curly
4 #The values of a dictionary can be of any type,
5 #but the keys must be of an immutable data type such as strings, numbers, or
6
7
8 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
9 print ("dict['Name']: ", dict['Name'])
10 print ("dict['Age']: ", dict['Age'])
dict['Name']: Zara
dict['Age']: 7
In [124]: 1 #If we attempt to access a data item with a key, which is not part of the di
2
3 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
4 print ("dict['Alice']: ", dict['Alice'])
---------------------------------------------------------------------------
<ipython-input-124-46fd1b2389b2> in <module>
KeyError: 'Alice'
dict['Age']: 8
<class 'dict'>
In [ ]: 1 #More than one entry per key not allowed. Which means no duplicate key is al
2 #When duplicate keys encountered during assignment, the last assignment wins
dict['Name']: Manni
[1 2 3 4 5]
<class 'numpy.ndarray'>
In [145]: 1 #2D
2 import numpy as np
3
4 arr = np.array([[1, 2, 3], [4, 5, 6]])
5
6 print(arr)
[[1 2 3]
[4 5 6]]
In [146]: 1 #3D
2
3 import numpy as np
4
5 arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
6
7 print(arr)
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
In [ ]: 1