You are on page 1of 4

1/23/23, 1:45 PM Untitled - Jupyter Notebook

In [2]: x = input()
y = input()

5
6

In [3]: x = int(x)
y = int(y)
x+y

Out[3]: 11

In [4]: #exponent(taking power)


print(5**2)

25

In [5]: #remainder
print(5%2)

In [10]: if x>y:
print("nice")

else:
print("not so nice")

not so nice

In [11]: num = 10
if (x==y and y==num): #and / or
print("correct")
else:
print("incorrect")

incorrect

In [20]: #list of number


numbers = [10,5,7,2,7,8,5,1,4]

sum = 0

#for loop
for count in numbers:
print(count,end=' ')
sum+=count

print ("\n" + str(sum))

10 5 7 2 7 8 5 1 4
49

In [31]: #list of strings


genre = ['pop','2.33','hiphop','lofi']

#iterate over the list using index
for i in range(4):
print("i like", genre[i])

i like pop
i like 2.33
i like hiphop
i like lofi

localhost:8889/notebooks/AI LAB/Untitled.ipynb# 1/4


1/23/23, 1:45 PM Untitled - Jupyter Notebook

In [32]: #if you don't know length of list


print("")
x = len(genre)
for i in range(x):
print("i like", genre[i])

i like pop
i like 2.33
i like hiphop
i like lofi

In [34]: #while loop


n = 10
sum = 0
i = 1

while i<=n:
sum += i
i+=1

print("The sum is",sum)

The sum is 55

In [35]: #1. Create Python List


areas = [10.0, 16.0,10.0]
#2. List with mixed data types
areas = [10.0, "BED", 20.0, "SOFA"]

#3 List of list
bedroom = [1,2,3]
hall = [4,5,6]
office = [7,8,9]

list_ = [bedroom,hall,office]

list_

Out[35]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [37]: numbers = [1,2,3,4,5,6,7,8,9]


print(numbers[3:8])
print(numbers[4:7])
print(numbers[2:])
print(numbers[:5])
print(numbers[:])

[4, 5, 6, 7, 8]
[5, 6, 7]
[3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [45]: numbers = [1,2,3,4,5,6,7,8,9]


numbers.append(10)
print(numbers)
numbers.append([11,12])
print(numbers)
numbers.extend([13,14])
print(numbers)
numbers.insert(3,55)
print(numbers)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, [11, 12]]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, [11, 12], 13, 14]
[1, 2, 3, 55, 4, 5, 6, 7, 8, 9, 10, [11, 12], 13, 14]

localhost:8889/notebooks/AI LAB/Untitled.ipynb# 2/4


1/23/23, 1:45 PM Untitled - Jupyter Notebook

In [56]: my_tuple = (1,2,3,4,5,6)


#nested tuple
my_tuple = (1,2,3,4,5,6,[7,8,9])
print(my_tuple)
#accessing list in tuple
print(my_tuple[6][1])
#tuple can be created without parenthesis
my_tuple = 'cat',2,3,4,'dog'
#unpacking tuple

a,b,c,d,e = my_tuple
print(a)
print(b)
print(c)
print(d)
print(e)
print("")
print(my_tuple.count(2))
print(my_tuple.index('dog'))

print(type(my_tuple))

(1, 2, 3, 4, 5, 6, [7, 8, 9])


8
cat
2
3
4
dog

1
4
<class 'tuple'>

In [63]: #dictionary
my_dict={}

#dictionary with integer keys
my_dict = {1:'apple',2:'ball','age': 26}

print(my_dict[1])
print(my_dict[2])
my_dict['age'] = 27
print(my_dict['age'])

apple
ball
27

In [ ]: squares = {1:1,2:4,3:9}


localhost:8889/notebooks/AI LAB/Untitled.ipynb# 3/4


1/23/23, 1:45 PM Untitled - Jupyter Notebook

In [72]: #SETS
my_set = {1,2,3,3,5}
print(my_set)

my_set.add(6)
print(my_set)

my_set.update([7,8,1])
print(my_set)

my_set.update([9,10],{11,22})
print(my_set)

my_set.remove(1)
print(my_set)

my_set.discard(2)
print(my_set)


#sets cannot have duplicates

#we can make set from a list
#set function
#we use set funciton to remove duplicates from a list
my_set = set([1,2,3,4,3,2])
print(my_set)

{1, 2, 3, 5}
{1, 2, 3, 5, 6}
{1, 2, 3, 5, 6, 7, 8}
{1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 22}
{2, 3, 5, 6, 7, 8, 9, 10, 11, 22}
{3, 5, 6, 7, 8, 9, 10, 11, 22}
{1, 2, 3, 4}

In [ ]: ​

localhost:8889/notebooks/AI LAB/Untitled.ipynb# 4/4

You might also like