You are on page 1of 14

6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [ ]: 1 # aaaaaaaa
2 # bbbnnnn
3 # cccccc

In [1]: 1 print('hello world')

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

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 1/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

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

In [84]: 1 float = 2.154327


2 format_float = "{:.2f}".format(float)
3 print(format_float)

2.15

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 2/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [85]: 1 float = 2.154327


2 format_float = "{:.3f}".format(float)
3 print(format_float)

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)

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 3/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

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")

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

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 4/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [108]: 1 for i in range(10):


2 num = int(input())
3
4 if num >=1 and num <=100:
5 if num%2==0:
6 print("even")
7 else:
8 print("odd")
9 else:
10 print("Enter a number between 1 to 100")
11 ​

even

34

even

21

odd

45

odd

odd

even

67

odd

78

even

76

even

54

even

In [109]: 1 fruits = ["apple", "banana", "cherry"]


2 for x in fruits:
3 print(x)

apple

banana

cherry

In [61]: 1 fruits = ["apple", "banana", "cherry"]


2 for x in fruits:
3 print(x)
4 if x == "banana":
5 break

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

In [62]: 1 def my_function():


2 print("Hello from a function")
3 ​
4 my_function()

Hello from a function

In [63]: 1 def my_function(fname):


2 print(fname + " Zaman")
3 ​
4 my_function("Mina")
5 my_function("Farhim")
6 my_function("Tina")

Mina Zaman

Farhim Zaman

Tina Zaman

In [135]: 1 # Function definition is here


2 def changeme( mylist ):
3 #"This changes a passed list into this function"
4 mylist.append([1,2,3,4]);
5 print ("Values inside the function: ", mylist)
6 return
7 ​
8 # Now you can call changeme function
9 mylist = [10,20,30];
10 changeme( mylist );
11 print ("Values outside the function: ", mylist)

Values inside the function: [10, 20, 30, [1, 2, 3, 4]]

Values outside the function: [10, 20, 30, [1, 2, 3, 4]]

User Input
In [38]: 1 input()

10

Out[38]: '10'

In [39]: 1 input("Enter your name : ")

Enter your name : ritu

Out[39]: 'ritu'

In [40]: 1 x = input("Enter your age : ")

Enter your age : 10

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 6/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [41]: 1 x

Out[41]: '10'

In [42]: 1 x = int(input("Enter your age : "))

Enter your age : 10

In [43]: 1 x

Out[43]: 10

In [51]: 1 a = int(input("Enter a number: "))


2 b = int(input("number 2: "))

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

In [71]: 1 import sys


2 sys.getsizeof(data)

Out[71]: 69

In [72]: 1 data.count("data")

Out[72]: 1

In [73]: 1 #index searching


2 data.find("data")

Out[73]: 8

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 7/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

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")

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

<ipython-input-77-50a047b1f2db> in <module>

----> 1 data.index("w")

ValueError: substring not found

In [78]: 1 data.upper()

Out[78]: 'I LOVEE DATA SCIENCE'

In [79]: 1 data.lower()

Out[79]: 'i lovee data science'

In [81]: 1 data.capitalize()

Out[81]: 'I lovee data science'

In [82]: 1 data.swapcase()

Out[82]: 'i LOVEE DATA SCIENCE'

In [83]: 1 data.title()

Out[83]: 'I Lovee Data Science'

In [86]: 1 data.split() #split in list

Out[86]: ['I', 'lovee', 'data', 'science']

In [87]: 1 data.split()[1]

Out[87]: 'lovee'

In [88]: 1 data.replace('data','ai')

Out[88]: 'I lovee ai science'

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 8/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

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)

Input your First Name : Jarin

Input your Last Name : Tasnim

Hello Tasnim Jarin

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

Out[91]: [1, 2, 3, 'ritu', True, (1, 2, 3)]

In [92]: 1 n[0]

Out[92]: 1

In [104]: 1 n[0:4]

Out[104]: [1, 2, 3, 'ritu']

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

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 9/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [114]: 1 #Updating Lists


2 list = ['physics', 'chemistry', 1997, 2000];
3 print ("Value available at index 2 : ")
4 print (list[2])
5 list[2] = 2001;
6 print ("New value available at index 2 : ")
7 print (list[2])
8 print(list)

Value available at index 2 :

1997

New value available at index 2 :

2001

['physics', 'chemistry', 2001, 2000]

In [115]: 1 #Delete List Elements


2 ​
3 list1 = ['physics', 'chemistry', 1997, 2000];
4 print (list1)
5 del list1[2];
6 print ("After deleting value at index 2 : ")
7 print (list1)

['physics', 'chemistry', 1997, 2000]

After deleting value at index 2 :

['physics', 'chemistry', 2000]

In [117]: 1 [1, 2, 3] + [4, 5, 6]

Out[117]: [1, 2, 3, 4, 5, 6]

In [118]: 1 ['Hi!'] * 4

Out[118]: ['Hi!', 'Hi!', 'Hi!', 'Hi!']

In [ ]: 1 ​

In [ ]: 1 ​

In [98]: 1 #tuple () use parentheses.


2 #ordered and immutable.

In [99]: 1 t = (1,2,3,"ritu",True,(1,2,3))

In [100]: 1 t

Out[100]: (1, 2, 3, 'ritu', True, (1, 2, 3))

In [101]: 1 t[0]

Out[101]: 1

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 10/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [103]: 1 t[0:4]

Out[103]: (1, 2, 3, 'ritu')

In [121]: 1 tup1 = ('physics', 'chemistry', 1997, 2000);


2 tup2 = (1, 2, 3, 4, 5, 6, 7 );
3 print ("tup1[0]: ", tup1[0])
4 print ("tup2[1:5]: ", tup2[1:5])

tup1[0]: physics

tup2[1:5]: (2, 3, 4, 5)

In [ ]: 1 #Updating Tuples


2 #Tuples are immutable which means you cannot update or change the values of
3 #You are able to take portions of existing tuples to create new tuples as th

In [122]: 1 tup1 = (12, 34.56);


2 tup2 = ('abc', 'xyz');
3 ​
4 # Following action is not valid for tuples
5 # tup1[0] = 100;
6 ​
7 # So let's create a new tuple as follows
8 tup3 = tup1 + tup2;
9 print (tup3)

(12, 34.56, 'abc', 'xyz')

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

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 11/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

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'])

---------------------------------------------------------------------------

KeyError Traceback (most recent call last)

<ipython-input-124-46fd1b2389b2> in <module>

3 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

----> 4 print ("dict['Alice']: ", dict['Alice'])

KeyError: 'Alice'

In [125]: 1 #Updating Dictionary


2 #You can update a dictionary by adding a new entry or a key-value pair,
3 #modifying an existing entry, or deleting an existing entry
4 ​
5 ​
6 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
7 dict['Age'] = 8; # update existing entry
8 dict['School'] = "DPS School"; # Add new entry
9 ​
10 print ("dict['Age']: ", dict['Age'])
11 print ("dict['School']: ", dict['School'])
12 print(dict)

dict['Age']: 8

dict['School']: DPS School

{'Name': 'Zara', 'Age': 8, 'Class': 'First', 'School': 'DPS School'}

In [132]: 1 #Delete Dictionary Elements


2 #You can either remove individual dictionary elements or clear the entire co
3 #You can also delete entire dictionary in a single operation.
4 ​
5 ​
6 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
7 del dict['Name'] # remove entry with key 'Name'
8 dict.clear() # remove all entries in dict
9 del dict
10 #print ("dict['Age']: ", dict['Age'])
11 print(dict)
12 ​

<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

In [134]: 1 dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}


2 print ("dict['Name']: ", dict['Name'])

dict['Name']: Manni

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 12/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [138]: 1 # program to display student's marks from record


2 student_name = 'Sobuj'
3 ​
4 marks = {'James': 90, 'Jules': 55, 'Arthur': 77}
5 ​
6 for student in marks:
7 if student == student_name:
8 print(marks[student])
9 break
10 else:
11 print('No entry with that name found.')
12 ​

No entry with that name found.

In [144]: 1 #numpy array 1D


2 ​
3 import numpy as np
4 ​
5 arr = np.array([1, 2, 3, 4, 5])
6 ​
7 print(arr)
8 ​
9 print(type(arr))

[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]]]

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 13/14


6/5/22, 12:30 PM Python Lab 1 - Jupyter Notebook

In [147]: 1 #Check Number of Dimensions


2 import numpy as np
3 ​
4 a = np.array(42)
5 b = np.array([1, 2, 3, 4, 5])
6 c = np.array([[1, 2, 3], [4, 5, 6]])
7 d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
8 ​
9 print(a.ndim)
10 print(b.ndim)
11 print(c.ndim)
12 print(d.ndim)

In [ ]: 1 ​

localhost:8888/notebooks/Python Lab 1.ipynb#If-else 14/14

You might also like