Aug 17-21, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch2/module1-day2.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab2.pipal.in for this training. Use notebook with name module1-day2.ipynb for today's session.
max, min, sum, len, int, str, float, type
print("Hello World!")
print("one", "two", "as", "many", "parameter")
print("one", "two", "as", "many", "parameter", sep="-")
opposite of printing output is getting some input from user
x = input("Enter value of x")
print(x)
type(x)
x
input is built in function which takes text input from user.
x = int(input("Enter integer value for x"))
x
int(x)
sorted([3, 2, 4, 1, 3, 4])
numbers = [76, 3, 5, 2, 4, 1,3, 5]
sorted(numbers)
sorted(numbers, reverse=True)
sorted?
sum([1, 2, 3, 4])
sentence = "These Are Some Wise Words"
sentence.startswith("This")
sentence.startswith("These")
startwith() # it can not be called like this
checking methods
sentence.endswith("words") # case sensitive
sentence.endswith("Words")
sentence.isupper() # all char upper case of not
sentence.islower() # all chars lower case or not
sentence.istitle() ## only first letter of every word is capital?
sentence.isalpha() ## whether it has only alphabets. space is not counted in alphabets
"ksajhdsadfds".isalpha()
sentence.isalnum()# alphabets and numbers
"user123".isalnum()
Transformation
"hello".capitalize()
sentence.upper()
sentence.lower()
"hello world".title()
sentence.rjust(50)
sentence.ljust(50)
sentence.center(50)
sentence.replace("Wise", "Foolish")
sentence
sentence[0] = "h"
These transofrmation methods will be used widely
sentence.split() # without parameters, it splits on whitespace
multiline = """line1
line2
line3"""
multiline.split()
poem = """Twinkle twinkle little star
how I wonder
what you are
"""
poem.split()
poem
poem.split("\n")
sentence.split()
words = sentence.split()
words
"-".join(words)
"_".join(words)
",".join(words)
",".join([1, 2, 3, 45])
",".join(["1", "2", "3", "45"])
poem = """Twinkle twinkle little star
how I wonder
what you are
"""
sentence1 = "He said"
sentence2 = "'I am fine'"
print(sentence2)
print(sentence1)
",".join([sentence1, sentence2])
print(",".join([sentence1, sentence2]))
multiline = """this is first
and sencond
and this one is last"""
multiline.split("\n")
lines = multiline.split("\n")
lines
print("\n".join(lines))
print("\n".join([sentence1, sentence2]))
poem.split("\n")
poem
poem.rstrip()
poem.rstrip().split("\n")
" hello this has trailing spaced on left".lstrip()
" hello this has triling spces on both sides ".rstrip()
" hello this has triling spces on both sides ".strip()
sentence = " helo method chianing! "
sentence.strip().split()[-1]
---------->-------->------> methods will be executed in this order
sentence = "These Are Some Wise Words"
"Wise" in sentence
"Wise" not in sentence
sentence
sentence.replace?
# help(str) show documentaion for str
help(sentence.replace)
Problems
username. How will you check if username is as per rules?>>> sentence = "Yet-another-sentence-with-nothing-in-it"
How can you transform this sentence such that there will be space between ever two words. >>> sep = "\\"
Folder names starting from c: drive till the folder containing executable 'python.exe' are given in a list.
>>> folders = ["C:", "Program Files", "python3.8"]
How will you make complete string fro complete path of python.exe? >>> path = "/home/vikrant/training/day1.html"
How will you find only name of file from given path?filename = "hello.xlsx"solution 1
username = "vikrant231"
username.isalnum()
username = "vikrant patil"
username.isalnum()
"2323".isalnum()
"2323 3434".isalnum()
solution 2
sentence = "Yet-another-sentence-with-nothing-in-it"
sentence.replace("-"," ")
sentence.split("-")
" ".join(sentence.split("-"))
solution 3
folders = ["C:", "Program Files", "python3.8"]
exe = "python.exe"
path = "\\".join(folders + [exe])
path
folders
folders + exe
folders + [exe]
"\\".join(folders + [exe])
solution 4
path = "/home/vikrant/training/day1.html"
path.split("/")
path.split("/")[-1]
solution 5
filename = "hello.xlsx" # week1.day1.xlsx
filename.split(".")
filename.split(".")[-1]
to find items
nums = [1, 2, 3, 4]
nums.index(3)
nums.index(2)
nums = [1, 1, 3, 2, 3, 4, 2, 1, 2, 3, 3]
nums.index(3)
nums.count(3)
nums.count(1)
methods to add items to list
empty = []
empty.append(1)
empty
empty.append(0)
empty
empty.append(1)
empty
empty.append(1)
empty
empty.insert(0, 23) # insert 23 at location 0
empty
empty.extend([0, 0, 0]) # this will add all items at end
empty
Method to remove
nums = [ 1, 2, 3]
nums.remove(2)
nums
nums.pop() # remove last item
nums
digits = [0, 1, 2, 3, 4, 5, 6]
digits.pop(2) # remove item at location 2
digits
digits.clear() # remove all
digits
chars = ["a","b","c","a","a","b"]
chars
chars.pop(2)
chars
chars.index('a')
chars.pop(0)
chars.index('a')
chars.pop(1)
chars.index('a')
chars.pop(1)
chars
some other manipulations
nums = [3, 2, 4, 1]
nums.sort() # sort in place
nums
nums.reverse() # reverse in place
nums
nums.copy() # this is same as nums[::]
x = [23 ,23, 2, 424]
sorted(x)
x
x.sort()
x
x.append?
x.append([1, 2, 3])
x
x.pop()
x
x.extend([1, 2, 3])
x
x
y = x
x.append(0)
y
x
z = x.copy()
x
z
x.append(-1)
z
x
+---------------+
| |
inputs ->----| square |--------> output
| |
+---------------+
def square(x):
sqrx = x**2
return sqrx
square
square(5)
def sumofsquares(a, b): # function defination starts with def, has to end with :
a2 = square(a) # next line has to be indented 4 spaces by convention
b2 = square(b) # all lines in this code block has to indented at same level
return a2 +b2 # finally return statement
sumofsquares(3,4) # this statement is also inside function
def sumofsquares(a, b): # function defination starts with def, has to end with :
a2 = square(a) # next line has to be indented 4 spaces by convention
b2 = square(b) # all lines in this code block has to indented at same level
return a2 +b2 # finally return statement
sumofsquares(3,4) # this is outside function
A function must have atlest one line in it
def donothing():
pass # empty statement
donothing()
A function can be written without return statement
def say_hello(name):
print("Hello", name + "!")
say_hello("python")
y = square(5)
y
square(4)
16
x = 10
y = 20
x+y
x*y
x**y
square(4)
square(5)
square(3)
sumofsquares(3, 4)
10