0% found this document useful (0 votes)
36 views

Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3/3/24, 9:03 PM Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

In [ ]: # if-else conditions

In [5]: a = 200
b = 200
if b>a:
print("b is greater than a")
elif a==b:
print("a and b are equal")
else:
print("a is greater than b")

a and b are equal

In [ ]: # while loops

In [6]: a = 4 #initializing
while a<10:
print(a, "is a less than 10")
a = a+1 #iteration a+=1

4 is a less than 10
5 is a less than 10
6 is a less than 10
7 is a less than 10
8 is a less than 10
9 is a less than 10

In [7]: # while - continue


a = 4
while a<10:
a+=1
if a==6:
continue
print(a)

5
7
8
9
10

In [8]: # while - break


a = 4
while a<10:
print(a)
if a==7:
break
a+=1

4
5
6
7

localhost:8888/notebooks/Downloads/Python Bootcamp - Week 3 - Day 1.ipynb 1/6


3/3/24, 9:03 PM Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

In [ ]: # for loops

In [11]: for a in range(4,11):


print(a, end = " ")

4 5 6 7 8 9 10

In [13]: # for loops with list


cities = ["Hyd","Bglr","Pun","Mum"]
for x in cities:
print(x, end = " ")

Hyd Bglr Pun Mum

In [14]: # zip() to iterate over paor of elements


names = ['Alice','Bob','Daniel']
age = [25,30,22]


for x,y in zip(names,age):
print(x,y)

Alice 25
Bob 30
Daniel 22

In [17]: # for - break


names = ['Alice','Bob','Daniel']
for x in names:
print(x)
if x=="Bob":
break

Alice
Bob

In [18]: # for-continue
names = ['Alice','Bob','Daniel','Charlie','John']
for x in names:
if x=="Charlie":
continue
print(x)

Alice
Bob
Daniel
John

localhost:8888/notebooks/Downloads/Python Bootcamp - Week 3 - Day 1.ipynb 2/6


3/3/24, 9:03 PM Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

In [19]: # given a list of numbers,


# iterate and print only those numbers which are divisible by 5
list1 = [10,20,25,21,34,55,60]
for x in list1:
if x%5==0:
print(x)

10
20
25
55
60

In [20]: # accept a number from user as input and print it's table until range(1,11)
a = int(input("Please enter a number: "))
for i in range(1,11):
product = a*i
print(product)

Please enter a number: 10


10
20
30
40
50
60
70
80
90
100

In [21]: # user defined function


def greet(name):
print("Hi, my name is " + name)

In [23]: greet("Bob")

Hi, my name is Bob

In [24]: # create a function to find the highest numbers out of 2 given numbers
def highest(x,y):
if x>y:
return x
return y

In [26]: highest(100,30)

Out[26]: 100

In [ ]: # lambda function

In [27]: highest = lambda x,y: x if x>y else y

localhost:8888/notebooks/Downloads/Python Bootcamp - Week 3 - Day 1.ipynb 3/6


3/3/24, 9:03 PM Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

In [28]: print(highest(10,5))

10

In [34]: # list comprehensions


a = [1,2,3,4,5]
square = [x**2 for x in a]
print(square)

[1, 4, 9, 16, 25]

In [31]: words = ['apple','banana','pear','mango']


length = [len(x) for x in words]
length

Out[31]: [5, 6, 4, 5]

In [33]: # dictionary comprehensions


words = ['apple','banana','pear','mango']
length = {x: len(x) for x in words}
length

Out[33]: {'apple': 5, 'banana': 6, 'pear': 4, 'mango': 5}

In [36]: a = [1,2,3,4,5]
square = {x: x**2 for x in a}
square

Out[36]: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

In [37]: original = {'a':1,'b':2,'c':3,'d':4}


new = {key:value**2 for key,value in original.items()}
print(original)
print(new)

{'a': 1, 'b': 2, 'c': 3, 'd': 4}


{'a': 1, 'b': 4, 'c': 9, 'd': 16}

In [ ]: # map, reduce, filter

In [39]: #map function


words = ['apple','banana','pear','mango']
length = list(map(len,words))
length

Out[39]: [5, 6, 4, 5]

In [40]: num = [1,2,3,4]


new = list(map(lambda x: x**2, num))
new

Out[40]: [1, 4, 9, 16]

localhost:8888/notebooks/Downloads/Python Bootcamp - Week 3 - Day 1.ipynb 4/6


3/3/24, 9:03 PM Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

In [44]: original = {'a':1,'b':2,'c':3,'d':4}



def square_value(value):
return value**2

squared = dict(map(lambda item: (item[0],square_value(item[1])),original.it

print(squared)

{'a': 1, 'b': 4, 'c': 9, 'd': 16}

In [45]: # filter function


a=[1,34,24,45,65,60]
b = list(filter(lambda x: x >24,a))
b

Out[45]: [34, 45, 65, 60]

In [46]: a=[-1,-34,-24,45,65,60]
b = list(filter(lambda x: x >0,a))
b

Out[46]: [45, 65, 60]

In [47]: # reduce
from functools import reduce
a = [1,2,3,4,5]
sum_result = reduce(lambda x,y: x+y, a)
sum_result

Out[47]: 15

In [48]: from functools import reduce


a = [1,2,3,4,5]
sum_result = reduce(lambda x,y: x*y, a)
sum_result

Out[48]: 120

In [50]: # python program to check whether given number is a multiple of both 5 and
num = int(input("Enter a number : "))
if ((num%5==0)and(num%7==0)):
print(num, "is divisible by 5 and 7")
else:
print(num, "is not divisible by 5 and 7")

Enter a number : 35
35 is divisible by 5 and 7

In [53]: # python program to display all the mutiples of 3 in the range of 10 to 50


for x in range(10,50):
if(x%3==0):
print(x, end= " ")

12 15 18 21 24 27 30 33 36 39 42 45 48

localhost:8888/notebooks/Downloads/Python Bootcamp - Week 3 - Day 1.ipynb 5/6


3/3/24, 9:03 PM Python Bootcamp - Week 3 - Day 1 - Jupyter Notebook

In [54]: # list comprehension


# given a list of product prices, come up with new list of discounted price
# each price is reduced by 10%

prices = [50,75,120,30,40]
new =[x*0.9 for x in prices]
new

Out[54]: [45.0, 67.5, 108.0, 27.0, 36.0]

In [55]: prices = {'laptop':12000, 'smartphone': 10000, 'Headphones': 1200,'Tablet':


new = {x:y*0.9 for x,y in prices.items()}
new

Out[55]: {'laptop': 10800.0,


'smartphone': 9000.0,
'Headphones': 1080.0,
'Tablet': 4500.0}

In [ ]: # Homework
# filter function
# list comprehension
# list products > 95
a = [80,120,95,150,110]
output = [120,150,110]

localhost:8888/notebooks/Downloads/Python Bootcamp - Week 3 - Day 1.ipynb 6/6

You might also like