Oct 25-31, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/arcesium-basic-oct/day1.html
© Pipal Academy LLP
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
2 + 3
3.0 + 3
2**100
1 + 2
3 - 5
2 * 5
2 ** 5
6/2
6//2
5//2
5/2
3.0 + 1.2
4.5 - 5
4.4 * 2
4.4**3
5**0.5
language = "python"
language
doublequoted = "double quoted string"
singlequoted = "single quoted string"
doublequoted
singlequoted
convers = 'he said, "I am fine"'
convers
both = "\"'"
both
tabbed = "name\tsurname"
print(tabbed)
print(both)
both
twoline = "first line\nsecond line"
print(twoline)
multi= """
this is frist line
second line
thrid line
..
.
you can have as many as you want
"""
print(multi)
s = """
hello
how's that
it"s good
"""
print(s)
'''
single quoted multiline string
hello
there
'''
s = "hello" "world"
s
unicode = "आ"
print(unicode)
empty = ""
digits = [1,2,3,4,5,6,7,8,9]
digits
digits[0]
digits[-1]
[A, B , C, D]
-> 0 1 2 3
-4 -3 -2 -1 <-
[1, 2, 3, 4, 5, 6, 7, 8, 9]
index 0 1 2 3 4 5 6 7 8
digits[2:6] #start at index 2, end at 6(exlude)
digits[:2] #take first two
digits[2:] #drop first two
a = 3
b = 4
c = "hello"
d = """
multi
line
string
"""
collection = [a, b, c, d]
print(collection[-1])
print(collection[0])
collection
data2d = [[1,2,3],[4,5,6],[7,8,9]]
data2d
data2d[0]
data2d[-1] # last row
data2d[0][0] # 0th element from 0th row
t = (1, 2, 3, 4, 5)
numbers = [1, 1, 2, 3, 5, 8]
numbers[0] = 0
numbers
t[0] = 0
players = ["David", "Alice", "Alex", "Lewis", "Anand", "Shiva"]
countries = ["USA", "UK", "USA", "UK", "India", "India"]
cont = set(countries)
cont
person = {"name":"Alice", "email":"alice@wonder.land", "age":13}
person["name"]
person["email"]
person["age"]
sum(numbers)
numbers
sum_ = 19
mean = sum_/6
data ={
"numbers" : numbers,
"sum":sum_,
"mean":mean
}
data
sum_
sum = [2,3,4,5]
sum_ = sum(numbers)
del sum
sum = [2,3,4,5]
sum_ = sum[2:5]
sum_
sum(2,3)
del sum
sum([3,4])
sum_
yes = True
yes
no = False
no
x = 2
x == 2
x == 3 # check if equal
x != 0 ## check if not equal
"hel" in "hello world"
nothing = None
nothing
print(nothing)
try this
pre = "foo"
post = "bar"
together = pre post
"foo" "bar"
pre = "foo"
post = "bar"
x = 4
y = 5
x +y
print(x,y)
3 + 4
numbers = [1,2,3,4,5,6]
sum_ = sum(numbers)
mean = sum_/6
def stats(numbers):
s = sum(numbers)
m = s/len(numbers)
return s,m
def add(x, y):
return x+y
add(2, 3)
stats(numbers)
len(numbers)
numbers
len("How many char are there in this string")
len(t) # t is tuple
t
len(person) # dictionary
person
cont
len(cont)
len(100)
int("100")
str100 = "100"
int(str100)
len(str100)
len(100)
float("1.2")
t
len(w)
str(1123)
sum([2,3,4,5,6])
max([56,23,1,789,12323])
int, str, float, len, max, min, sum
problem
x = 2**100
strx = str(x)
len(strx)
problem
count_digits which will return number of digits in given numberdef count_digits(num):
return len(str(num))
count_digits(2**1000)
problem
twice which returns twice of a numberbonus problem
sumnaturals which returns sum of first n natural numbers
hint
list(range(n))list(range(10))
def twice(x):
return 2*x
def twice_(x):
print(2*x)
twice(45)
twice_(45)
x = 42
x2 = twice(x)
x2
x2_ = twice_(x)
x2_
print(x2_)
twice(twice(2))
twice(4)
8
twice_(twice_(2))
twice_(None)
2*None
numbers
singlequoted
t
person
numbers.count(1)
numbers.append(7)
numbers
numbers.pop()
numbers
numbers + numbers
t + t
numbers.pop()
numbers[0] = -1
singlequoted
singlequoted.count(" ")
singlequoted.lower()
singlequoted.upper()
singlequoted.split(" ")
singlequoted.split()
"A,B,C,D".split(",")
singlequoted.split(":")
multi = """
hello
world
this has no trailing space
"""
multi.split("\n")
multi.strip().split()
x = "name:python"
x.split(":")
x
x.replace("python","english").replace("name", "NAME")
singlequoted.startswith("hello")
filename = "hello.py"
filename.endswith(".py")
singlequoted.center(100)
"*"*20 + singlequoted + "*"*20
"python" in x
".py" in filename
problems
write a function which decorates string as header of your file as given below
>>> decorate("some header")
********************* SOME HEADER **********************
Write a function which tells whether given file is python file or not
>>> is_python_file("system.dll")
False
>>> is_python_file("hello.py")
True
Write a function to find extension of a file.
>>> extension("system.dll")
dll
>>> extension("system.exe")
exe
Write a function count_zeros which counts how many zeros are there in a number?
>>> count_zeros(1000)
3
x = [1,2,3,4]
x[-1]
def decorate(header):
pre = "*"*20
return pre + " " + header.upper() + " " + pre
decorate("Hello world")
def is_python_file(filename):
return filename.endswith(".py")
is_python_file("system.dll")
is_python_file("hello.py")
def extension(filename):
return filename.split(".")[-1]
extension("python.exe")
extension("hello.py")
extension("hello.py.tgz")
"_".join(["hello", "world"])
filename = "hello.py.tgz"
tokens = filename.split(".")
tokens
tokens.pop()
tokens
".".join(tokens)
help(filename.split)
filename.split(".")[-1].pop()
s = " there is this sentence with some trailing spaces, one more sentence "
s.split(",")
s.strip().split(",")
def count_zeros(n):
return str(n).count('0')
count_zeros(1000)
l = [1, 1, 1, 2, 3, 5, 6]
l.count(1)
l.insert(0, -1)
l
l.reverse()
l
l.sort()
l
l.sort(reverse=True)
l
words = ["one", "two", "three", "four", "five"]
sorted(words)
sorted(words, reverse=True)
def fun():
print("Fun!")
fun
x
a = 2
a
fun
a()
a
fun
fun()
b = a
b
aliasfun = fun
aliasfun
aliasfun()
def square(x):
return x*x
def sumofsquares(x, y):
return square(x) + square(y)
def cube(x):
return x*x*x
def sumofcubes(x,y):
return cube(x) + cube(y)
def sumof(f, x, y):
return f(x) + f(y)
sumof(square, 2, 5)
sumofsquares(2, 5)
words
max(words)
max(words, key=len)
sorted(words)
sorted(words, key=len)