Dec 07-11, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch3/module1-day2.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab.pipal.in for this training. Create a notebook with name module1-day2.ipynb for today's session
Functions are nothing but combining multiple statements together and giving it a name. so that all those statements can be executed at once whenever we need it.
text = "This is a long string with some length"
text[100]
len(text) # here len is function name, text is argument/parameter of the function
len("Vikrant") # litteral string
len("Any string can work")
name = "Vikrant"
len(name) # passing variable as argument/parameter
ones = [1, 1,1, 1, ]
ones
len(ones) # length of list
len([1, 2, 3, 4, 5, 1])
len((0, 0, 256))
person = {"name":"alice",
"age":13,
"email":"alice@wonder.land"}
len(person)
x = 10
type(x)
y = 1.2
type(y)
name
type(name)
type(ones)
type((1, 2, 3, 4))
type(person)
x = 10000
statement = "There are many people in this town, do you know how many? " + x
"There are many people in this town, do you know how many? " + "10000"
"There are many people in this town, do you know how many? " + str(x)
str(232) # covert the data / argument into text/string
str(1.2)
str([1, 2, 2])
int("42") # convert string into integer
int("hello") # only things which can be converted into integer will work
float("2.3")
float("hello")
float("1")
float(45)
max([45, 56, 67, 78, 84, 2, 4, 5])
numbers = [23,56, 23, 42, 5, 1, 1]
max(numbers)
min(numbers)
sum(numbers)
sum([1.2, 2, 2,3, 2.2, 1.1])
len,type, str, int, float, min, max, sum these are few built-ins
min((1, 2, 3, 4, 5))
f = 1.2 ## Floating point standard causes the precision
len
max
max = (1, 2, 3, 4)
max
max([2, 3, 4, 5])
x = 10 # <-----
x = 20
del max # this will delete
[1, 2, 3, 4]name[0], numbers[0], color[0], stock['name']len([1, 2, 3, 4])
# len(...something) # function call , we used ()
# len( [1, 2, 3, 4] ) # argument to function is a list created using []
Problems:
sum(["a","b","c","d"])3**42
len(3**42)
incomes = [123330, 250000, 45555, 232130, 111134]
total_income = sum(incomes)
total_income
highest_income = max(incomes)
del max
highest_income = max(incomes)
highest_income
3**42
str_num = str(3**42)
str_num
len(str_num)
"a" + "b"
sum(["a","b","c","d"])
"a" + "b"
'0' + "a"
0 + "a"
sum([1, 2, 3])
sum([0])
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[2:6:2] # start 2 (inclusive) end at 6 (exlude this index) at steps of 2
digits[2:6:1]
digits[2:6]
digits
digits[0]
digits[3]
digits[0:4]
digits[0:7:2]
name = "vikrant"
name[0::2]
digits
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
digits[1:6]
digitis [3:6]
Make note if some default values
digits[:] # start =0 , end => length of that list/string ... making a copy
digits[3:] # start = 3, end ..till end... drop first 3
digits[:3] # start =0, end 3 .. this mean ... take first 3
digits[::1]
digits[::2]
digits[::3]
digits[::-1] # start at end and go till initial pos in reversed fashion, reverse the list
digits[:5] # take first five
digits[5:] # drop first five
digits[::2] # take alternate item
digits[::-1] # reverse the list
digits[3:8:-1]
digits[::-1] # python is taking decision of making the defaults here
digits[8:3:-1]
print("Hello World!")
name
print("Hello", name)
print("Hello", name,"how are you doing?") # three arguments
x, y, z = 1, 2, 3
print(x, y, z)
print("Hello " + name + " " + "How are you doing?") # single argument
name # is variable
"Hello" # is litteral
name = "Vikant"
name
'name' # this is not variable.... this is a litteral with 'name'
print(name)
print('name')
print(3)
x = 5
print(x) # this variable
print(5) # this is litteral
5
x = input("Enter value of x")
x
type(x)
i = input("Enter number of iterations")
i
ints = input("Enter comma sepearted ints")
ints
sorted([4, 5, 6, 2, 5,11, 2])
incomes
sorted(incomes) # this creates a new list in sorted form, it will not change original list
incomes
sorted(incomes, reverse=True)
incomes
len([1, 2, 3])
sentence = "These are few wise words"
sentence.startswith("These") # this will tell me whether sentence starts with "These"
sentence.startswith('these')
sentence.endswith('x')
sentence.endswith("words")
sentence.isupper()
sentence.islower()
"alllower".islower()
help(sentence.isascii)
help(sentence.center)
help(sentence.center)
sentence.istitle()
help(sentence.isalpha)
"a".isalpha()
"a b".isalpha()
sentence.isalnum()# whether the string has only alphabets and digits
"user123".isalnum()
sentence.capitalize()
"vikrant".capitalize()
sentence.upper()
sentence.lower()
sentence.title()
len(sentence)
sentence.title()
title()
sentence.title()
len
len(sentence)
len([1, 2, 3, 4])
len({'x':1})
sentence.title()
"vikrant".title()
sentence
"vikrant"
x = 10
y = 10
x
y
id(x)
id(y)
x
x = [1, 2, 3]
y = [1, 2, 3]
"hello"
x
y
x.append(1)
x
y.append(2)
y
x
function(arg, arg1)
object.method(args)-> might return result or might change object
sentence
sentence.split() # splits the string into multiple strings using empty spaces
words = sentence.split()
words
words[-1]
words[2]
ints = input("Enter comma separated ints")
ints
ints.split(",")
'[1,2,3,4]'
words
" ".join(words)
",".join(words)
"_".join(words)
Problems:
text = "Yet-another-sentence-with-nothing-in-it'
How can you transform it such that there will be space beween two words.
sep = "\\" escapce char
Thers is list of folders from C:folders = ["C:", "Program Files", "python3.9"]
can you make a string for complete path to python installation folder? "c:\Program Files\python3.9"
path = "/home/vikrant/training/module-day1.ipynb"
can you find out only filename?
filename = "hello.xlsx"