String, Len, Extend, Append functions
string = "geeks"
print(len(string))
# prints 5
----------------------------
string = "geeks for geeks"
print(len(string))
# Length of below string is 15
----------------------------
x = [1,2,3,4,5]
print(len(x))
# 5
----------------------------
x[:3]
#prints [1, 2, 3]
----------------------------
x = [1,2,3,4,5,6,7,8,9,10]
x[:3]
#[1, 2, 3]
----------------------------
x[:-3]
# [1, 2, 3, 4, 5, 6, 7] ignored last 3
----------------------------
x[:+3]
# [1, 2, 3] takes only first 3
----------------------------
x.extend([22,23])
print(x)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 23]
----------------------------
x.append([25,26])
print (x)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 23, [25, 26]]
----------------------------
y = [44,45,46]
print (x,y)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 23, [25, 26]] [44, 45, 46]
Tuples and Dictionary
(age, income) = "22,25000".split(',')
print (age)
print (income)
# 22
# 25,000
----------------------------
player = {}
player['height'] = "6.2"
player['name'] = 'John Donson'
player['age'] = '22'
----------------------------
print(player['age'])
print(player['name'])
# 22
# John Donson
----------------------------
print(player['Hobby'])
// Gives Error
----------------------------
print(player.get("Hobby"))
// Returns None
functions , Lambda , For Loop
def squarethis(i):
return i*print(squarethis(3))
// Prints 9
----------------------------
def callthisfunc( functname, i):
return functname(i)
print (callthisfunc( squarethis, 4))
// Prints 16 => which is 4*4
----------------------------
print (callthisfunc(lambda x: x*100, 4))
// returns 400
// Lambda allows us to define return type
----------------------------
if 1 is 4:
print ("1 is 4")
elif 1 > 4:
print ("1 is more than 4")
elseif 1 < 4:
print ("1 is less than 4")
else:
print ("Out of my head !")
// Should print the last line
----------------------------
for x in range(5):
print("hello")
// prints hello 5 times
----------------------------
x = 0
while (x<5):
print ("hello ")
print (x)
x += 1
// prints hello 1 .. it goes till 5 times
----------------------------
agegroup = [10,20,30,40,50]
for number in agegroup:
if number < 31:
print (number)
// it prints 10, 20, 30