Apr 15-18, 2019 Vikrant Patil
These notes are available online at http://notes.pipal.in/2019/arcesium_basic_apr/day1.html
© Pipal Academy LLP
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
1 + 1
**integers**
integers
2 - 3
2 * 3
2 ** 3
2 ** 1000 # power
5 / 2 #division operator
5 // 2 #integer division
5 % 2 # mod
floats
1.6 *3
1.6 - 1
1.5**5
string
"hello" + " " + "world"
'hello' + ' ' + "arcesium"
first = "Vikrant"
last = "Patil"
print(first)
first
Rules for variable names
x = "text data"
x1 = x + x
x2 = "another text"
x
x1
x2
x + x1
star = "*"
fivestar = star*5 #it repeates the string 5 times
fivestar
multiline = "first line\nsecond line"
print(multiline)
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.
"""
poem1 = '''
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.
'''
print(poem)
print(poem1)
n = 2
n
print(n)
multiline
print(multiline)
dialog = 'he said, "I am fine!"'
print(dialog)
statement = "it's his style"
print(statement)
multi = "first line\nsecond line"
multi1 = """first line
second line"""
print(multi)
print(multi1)
dialog1 = "he said, " '"he' "'s" ' way of doing thigs!"'
print(dialog1)
"first" "second" "third"
x x1 x2
2x = 2*x
empty = ""
Lists
digits = [0, 1,2,3,4,5,6,7,8, 9]
digits[0]
digits[9]
digits[-1]
digits[-2]
----> 0 1 2 3
data 'A' 'B' 'C' 'D'
-4 -3 -2 -1 <-----
digits
digits
digits[0] = -1 # lists allow modification of data
digits
chars = ["A","B", "C"]
chars
mixed = [1, 2, 3, 1.2, "string2", "hello"]
mixed[-1]
col1 = [1,2,3,4]
col2 = ["A","B","C","D"]
col3 = [1, 4, 9, 16]
table = [col1, col2, col3]
table
table[0] # 0th column
table[-1] # last column
table[1]
table[0][0] #0th column , 0th row
table[0][-1] #0th column , last row
tuples sibling of list
point = (1, 0, 5.6) #(x, y, z) coordinates
point[0]
point[-1]
point[1] = 1 # tuples do not allow modification of data
addpoint = point + point # concatenates
addpoint
point
(1, 1, 0)*5 # repeates it five times
dictionaries
person = {"name":"Vikrant", "email":"vikrant@example.com", "company":"Pipal Academy"}
person['name']
person['email']
person['launguage']
digits[12]
company_profile = {"name":"HCL", "ticker":"hcl", "value":1001.0, "exchange":"BSE"}
company_profile['name']
company_profile['value']
company_profile['value'] = 1002.3
print(company_profile)
sets sets are collection which is unique. no repetaion allowed
digits = {1, 2, 3, 4, 6, 6, 7}
digits
boolean
yes = True
no = False
yes
no
1 + 2
x
x1
x == x1 #equality operator checks if two items are same
inttypes = {True:"even", False:"odd"}
inttypes[True]
Dictionaries can have hashable keys (int, boolean, string, tuple) and any value.
nothing
nothing = None
Functions are predefined actions which may need some inputs. So these actions can be called again again and with different inputs
digits = [0,1,2,3,4,5,6,7,8,9]
len(digits) # len is called as function. digits is input for this function
items = ["A","B","C"]
len(items)
len(point) # it can work for tuples, gives length of tuple
len(company_profile) # it can work for dictionary, gives length of dictionary, number of keys
len(poem) # number of charecters in string..includes all charecters like space, tab, new line
"2" # this is a string
"2" + 2
two = "2"
int(two) # int is a function which converts text into integer
float("1.2") # float is a function which converts text into float
str(100) # str converts the object to string format
str(digits)
sum([1,2,3,4,5,6])
sum([1.1,2.3, 3.5])
sum(1, 2)
sum([1,2,3,4])
len("2")
len(2)
len(digits)
len(point)
len()
len = 5
len(digits)
del len
len(digits)
problem
2**1002**100
x = 2**100
len(str(x))
sx = str(x)
len(sx) # these two statements are same as above statement
def add(a, b):
s = a + b
return s
add(2, 4)
add("hello", "world")
def say_hello(p):
print("Hello " + p)
say_hello("Arcesium")
def line():
print("*"*30)
line()
x = add(5,6)
x
s = say_hello("Arcesium")
s
print(s)
say_hello("Hyderabad")
city = "Pune"
say_hello(city)
say_hello(pune)
problem
count_digits which takes an integer as input and returns number of digits in that integer
>>> count_digits(100)
3
def count_digits(n):
sn = str(n)
return len(sn)
count_digits(2**100)
count_digits(2**1000)
count_digits("234434")
add(3,4)
add(3)
add(1, 2, 3)
{1, 2, 3, 4} + {1, 5, 6}
s = {1,1,1,2,3,34,54,1,2,3,3}
s
s = {54, 34, 2, 2, 1, 1}
s
def twice(x):
return 2*x
def twice_(x):
print(2*x)
twice(4)
twice_(4)
twice(twice(4))
twice_(twice_(4))
range(0,10)
list(range(0,10))
list(range(10))
list(range(1,10))
list(range(1,20,3))
def naturals(n):
return range(1,n+1)
list(naturals(20))
def sum_naturals(n):
return sum(naturals(n))
sum_naturals(100)
def square(x):
return x*x
square
square(4)
square
x = 1
y = x
y
sqr = square
sqr
sqr(4)
square(4)
del square
square(4)
sqr(4)
def sqaure(x):
return x*x
def sumofsquares(x, y):
return sqaure(x) + sqaure(y)
def cube(x):
return x*x*x
def sumofcubes(x, y):
return cube(x) + cube(y)
def sumof(x,y, func):
return func(x) + func(y)
sumofsquares(2, 3)
sumof(2, 3, sqaure)
sumof(2, 3, cube)
sumofcubes(2, 3)
max([2,34, 545,12,12,1])
words = ["one", "two", "three", "four", "five"]
words
max(words)
max(words , key=len)
records = [
("HCL", 800.5, 2),
("HAL", 55.54, 5.4),
("INFY",1000.4, -4),
("TATA", 100, 1.3),
("RELIANCE", 707, 3)
]
## 0th column is name
## 1st column is value
## 2nd column is gain
max(records)
def get_value(row):
return row[1]
def get_gain(row):
return row[2]
max(records, key= get_value)
max(records, key=get_gain)
min(records, key=get_gain)
sorted(records)
sorted(records, key=get_value)
add
add(2,3)
sorted(records, key=get_value, reverse=True)
help(sorted)
records = [
{"name":"HCL", "value":800.5, "gain":2},
{"name":"HAL", "value":55.54, "gain":5.4},
{"name":"INFY","value":1000.4, "gain":-4},
{"name":"TATA", "value":100, "gain":1.3},
{"name":"RELIANCE", "value":707, "gain":3}
]
def get_value(r):
return r['value']
max(records, key=get_value)
text = "Lets have THIS sentence as random text"
text
type(text)
text.lower()
text.upper()
text
text.split(" ")
text.split()
multi = """
text with \t
lots of white spaces
"""
multi.split(" ")
multi.split()
def make_words(text):
return text.split()
make_words(poem)
text.lower()
text.upper()
text.capitalize()
text.lower().count("e")
trailing = " dasdasdas asdasdasd sdasd "
tailing.strip()
hello_template = "Hello Mr. NAME"
hello_template.replace("NAME", "Amit")
text.startswith("lets")
text.lower().startswith("lets")
text
text.endswith("xt")
filesname = "Hello.py"
def is_python(name):
return name.endswith(".py")
is_python(filesname)
is_python("windows.exe")
text.center(50)
text.ljust(50)
text.rjust(50)
words
" ".join(words)
"_".join(words)
"\\".join(["c:", "programs", "anaconda"])
ones = [1, 1, 1, 1]
ones.append(0)
ones
ones.append(10)
ones
ones.remove(10)
ones
ones.pop()
ones
ones.append(-1)
ones
ones.pop(0) # pop 0th element
ones.remove(0)
ones
ones.remove(1)
ones
ones.extend([1, 1, 1, 0, 0])
ones
x = [1, 1, 1]
x + [1,2,3]
x
digits
numbers = [3,7,2,9,3,89,37,1]
sorted(numbers)
numbers
numbers.sort()
numbers
numbers.reverse()
numbers
numbers.index(7)