primes = [2, 3, 5, 7, 11, 13]
primes[0]
2
primes[1]
3
for prime in primes:
print(prime*prime)
4 9 25 49 121 169
for i in primes:
print(i*i)
4 9 25 49 121 169
words = "one two three four five six seven".split()
print(words)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']
for word in words:
print(word[0])
o t t f f s s
for w in words:
print(w[-1])
e o e r e x n
classroom = {
"Nisha":1,
"Indraraj":2,
"Kaustubh":3,
"Renuka":4,
"Sameer":5,
"Shyreyas":6
}
print(classroom)
{'Nisha': 1, 'Indraraj': 2, 'Kaustubh': 3, 'Renuka': 4, 'Sameer': 5, 'Shyreyas': 6}
for student in classroom:
print(student, classroom[student])
Nisha 1 Indraraj 2 Kaustubh 3 Renuka 4 Sameer 5 Shyreyas 6
for char in "this is some string":
print(char) #print on newline
t h i s i s s o m e s t r i n g
for char in "this is some string":
print(char, end=",") #print on newline
t,h,i,s, ,i,s, ,s,o,m,e, ,s,t,r,i,n,g,
primesqurs = []
for p in primes:
primesqurs.append(p*p)
print(primesqurs)
[4, 9, 25, 49, 121, 169]
for item in classroom:
print(item)
Nisha Indraraj Kaustubh Renuka Sameer Shyreyas
Questions
emptydict = {}
data = {"a":23,"b":2,"c":45}
for item in classroom:
print(item.upper())
NISHA INDRARAJ KAUSTUBH RENUKA SAMEER SHYREYAS
for item in data:
print(data[item]**2)
529 4 2025
sqr_data = {}
for item in data:
sqr_data[item] = data[item]**2
print(sqr_data)
{'a': 529, 'b': 4, 'c': 2025}
Question
product which will compute product of all items from a list.
>>> product([3,4,1])
12
factorial which will find factorial of a number!
>>> factorial(5)
120
findlens which will find length of every word from given list of words.
>>> findlens(["one","two","three"])
{"one":3,
"two":3,
"three":5}
def square(x):
return x*x
def sum_of_squares(x, y):
return square(x) + square(y)
def product(numbers):
p = 1
for n in numbers:
p = p*n
return p
product([4, 5, 6, 7])
840
product([1, 2, 3, 4])
24
def factorial(n):
return product(range(1, n+1))
factorial(5)
120
factorial(20)
2432902008176640000
def findlens(words):
lens = {}
for word in words:
lens[word] = len(word)
return lens
findlens("hello this is some sentence for testing my function".split())
{'hello': 5,
'this': 4,
'is': 2,
'some': 4,
'sentence': 8,
'for': 3,
'testing': 7,
'my': 2,
'function': 8}
words = "hello this is some sentence for testing my function".split()
findlens(words)
{'hello': 5,
'this': 4,
'is': 2,
'some': 4,
'sentence': 8,
'for': 3,
'testing': 7,
'my': 2,
'function': 8}
True
True
False
False
"string"
'string'
x = 4
x == 1
False
x == 4
True
x != 4
False
x != 0
True
x < 4
False
x >= 5
False
words
['hello', 'this', 'is', 'some', 'sentence', 'for', 'testing', 'my', 'function']
"sentence" in words # you can directly check existence of item inside a list
True
"logic" in words
False
"Niket" in classroom # check is the item in left side exists in keys of the dictionary
False
"Nisha" in classroom
True
text = "Let me write some essay here using python"
"some" in text
True
text.isupper()
False
a non empty string, non empty list and a non empty dictionary results as True in if condition while empty string, list, dictionary results in False in if condition
if "logic" in text:
pass # empty statement
else:
print("hey ya.. you are here!")
hey ya.. you are here!
numbers = list(range(1, 21))
def filterodd(nums):
odds = []
for n in nums:
if n % 2 == 1:
odds.append(n)
return odds
filterodd(numbers)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
def filterodd_(nums):
odds = []
for n in nums:
if n%2 == 1:
odds.append(n)
return odds #check indentation! this has to be at for level
filterodd_(numbers)
[1]
Question
find_words_of_len to find all words of given length from given list.
>>> words = ["one", "two", "three"]
>>> find_words_of_len(words, 3)
["one",'two"]
unique which will remove duplicate entries from the list.
>>> unique([1, 1, 2, 2, 1, 2, 3, 4, 5, 6, 6, 8])
[1, 2, 3, 4, 5, 6, 8]
list(set([1, 1, 2, 3, 3, 3, 4, 5, 6]))
[1, 2, 3, 4, 5, 6]
Take home Question
urls = ['www.abrakadabra.com/dccEcB/EGdd',
'www.abrakadabra.com/gADFeD/bcAF',
'www.abra.com/AGadbb/eagE',
'www.dabra.com/cffdfD/FCAD',
'www.abra.com/GFGaBE/dcfc',
'www.abra.com/gaFegG/Bdaf',
'www.abrakadabra.com/aGabaf/EEfa',
'www.dabra.com/ceEgFD/bGgc',
'www.dabra.com/bDEffC/bcEA']
min2 which find minimum from given two numbers. Also write a function min3 which can find minimum number from given 3 numbers. Do not make use of bulit in min function.def unique(items):
unique_items = []
for item in items:
if item not in unique_items:
unique_items.append(item)
return unique_items
unique([1,1,1,2,2,3,3,4,5,4,5,4,6])
[1, 2, 3, 4, 5, 6]