Nov 26-30, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/arcesium-basic-nov/day1.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
2 + 3
200000 * 20023232323
print("Hello World!")
2 + 3
2 -4
5 / 2 #division
5 // 2 # this is integer division
5 ** 5 # power
4 * 5 # multiplication
2**1000 # you can have realy big integers
10%2 #modulus ... reminder when divided by 2
9%2
1/3
0.5 * 10
0.5 ** 5
"This is string ...text data"
sentence = "Hello World!"
sentence
print(sentence)
sentence = "hello world" + 2
"hello " + "world!"
greeting = "Hello" " World"
greeting
name = "Arcesium"
name
name + greeting
name greeting
name + greeting
name + " " + greeting
twolines = "one\ntwo"
print(twolines)
twolines
import this
poem = "The Zen of Python, by Tim Peters\nBeautiful is better than ugly."
poem
poem = """
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
poem = "The Zen of Python, by Tim Peters\nBeautiful is better than u
"""
poem
print(poem)
double = "I have a string with ' quote in it"
double
single = 'A string with a " qoute in it'
single
s = "A string with a \" qoute in it"
s
star = "*"
fivestar = star * 5 # multiplication just replicates the string 5 times
fivestar
numbers = [1,2,3,4,5,6,7,8,9]
numbers[0]
numbers[-1]
-> 0 1 2 3 4 5
A B C D E F
-6 -5 -4 -3 -2 -1 <-
l = [1, 2, "A", "Hello", "World"]
l[0]
l[-1]
l
l[0] = -1
l
Tuples are siblings of lists, it works exactly as list except that it does not allow any modification after creation.
t = (1, 2, 3, 4, 5)
t[0]
t[-1]
t[0] = 878
x = 3
del x
lists are collections which can be accessed by indices while dictionaries are collections which can be accessed by names
person = {"name":"alice", "age":12, "language":"English"}
l = ["alice", 12, "English"]
person['name']
person['age']
person['language']
person1 = {"name":"David",
"age":50,
"launguage":"English"
}
person1['name']
person['name']
persons_ = {"names":["Alice", "David"],
"ages":[12,50],
"languages":["English", "English"]
}
persons = [ person, person1]
persons_['names'][0]
persons_['names'][1]
persons_['names']
persons[0]['name']
persons_['names'[1]]
sentence
sentence[0]
'names'[1]
problem Represent stocks using lists and dictionaries. A stock has attributes
represent it for 4 different stocks
name ticker value volume
Infosys infy 1000 500
Tata tata 500 50
Reliance reliance 700 100
x = 2 + 3
x, y = 2*2 , 2**4
x
y
x,y = 2*2,2**4
I = {"ticker":"infy", "value":1000, "volume":500}
T = {"ticker":"tata", "value":500, "volume":50}
R = {"ticker":"reliance", "value":700, "volume":100}
stocks = {"Infosys":I,
"Tata":T,
"Reliance":R}
stocks["Tata"]['ticker']
stocks["Tata"]['value']
stocks["Tata"]['volume']
yes = True
no = False
yes
yes or no
yes and no
2 > 3
no and no
stocks['Tata']['value'] > 200
stocks['Tata']['value'] > 200 and stocks['Tata']['volume'] > 10
"Tata" in stocks
"Citi" in stocks
2 in [1,2,3,5,7,11]
"Citi" not in stocks
stocks.keys()
person
"Tata" in stocks
stocks['Tata']
print(stocks['Tata'])
None
x
del x
x
x = None
print(x)
x
2 != 3
2 == 2
l
len(l) #list
len(stocks) # dict
len("Hello World!") # string
len(t) # tuple
int("2")
str(2)
float("2.3")
fs = "2.3"
fs + 2
float(fs) + 2
l = [1,2,3,4,5]
n = len(l)
len(str(100))
len(str(100))
len('100')
3
list((1,2,3,4))
list(range(0,10))
list(range(10))
list(range(1,10))
list(range(1,20, 2))
print(range(1,20, 2))
items = [("name", "alice"),("age",12), ("language","english")]
items[0]
dict(items)
dict(range(5))
list(range(5))
items = [["name", "alice"],["age",12], ["language","english"]]
dict(items)
tuple(range(5))
sum(range(1,101))
2**100 # how many digits are there?
def count_digits(n):
s = str(n)
digits = len(s)
return digits
count_digits(100)
count_digits(2**100)
def add(x,y):
return x+y
add(3,4)
add([1,2,3],[5,6])
add("hello", " world")
[1, 2, 3] + [1,1,1]
x = 3
add(6,7)
x
problem
>>> sumnatural(10)
55
def twice(x):
return 2*x
def twice_(x):
print(2*x)
twice(3)
twice_(3)
y = twice(3)
z = twice_(3)
print(y)
print(z)
twice(twice(4))
twice_(twice_(4))
def sumnaturals(n):
return sum(range(1,n+1))
sumnaturals(100)
name = "ARCESIUM"
name
name.lower()
name1 = name.lower()
name1.replace("a","A")
name
name.lower()
"xyz".upper()
random = "I have a random string to do some operations"
random.count("o")
random.count("ing")
random.split(" ")
csvline = "a,b,c,d,e,xyz,hello"
csvline.split(",")
poem
words = poem.split()
len(words)
poem.lower().count(" ")
poem.lower().count("\n")
len(poem) # alphabet count which includes spaces
words[0]
poem[0]
len(poem.replace(" ","").replace("\n","").replace("\t","")) # alphabet count without spaces
random.startswith("some")
random.startswith("I")
filename = "hello.py"
filename.endswith(".py")
greeting
greeting.center(100)
greeting.ljust(100)
greeting.rjust(100)
problem
2**1000