Python Basic tuts
Lecture 4
String And File Manipulation .
String Manipulation is a formatting method for string. Since we know , that Strings are a type of data , thus we need some formatting technique to manipulate it.
Concatenation. - which is a technique to add two strings together.
str1 = 'Hello'
str2 = 'ABC'
str1 + str2
'HelloABC'
Here we can see that , there is no space , as we haven’t added it . To add a space , we need to add a space manually.
str1 + ' ' + str2
'Hello ABC'
Here, we can see that, this method is useful , for shorter texts or strings , but when we have a larger string or text to manipulate, adding spaces, new lines etc can be a very tedious task. Thus we need to have a second option intact, which is formatting.
Formatting is just a technique to manipulate the string all at once.
string = 'Hello, my name is {} . And I am here to learn.'
names = ['A','B','c','D','E','F','G','H']
for name in names:
print(string.format(name))
Hello, my name is A . And I am here to learn.
Hello, my name is B . And I am here to learn.
Hello, my name is c . And I am here to learn.
Hello, my name is D . And I am here to learn.
Hello, my name is E . And I am here to learn.
Hello, my name is F . And I am here to learn.
Hello, my name is G . And I am here to learn.
Hello, my name is H . And I am here to learn.
Here , we can see that, the format function helped us save a lot of time. If we have used the concatenation method , we would have been in trouble as the method was too long and time consuming for the manipulation.
string = 'Hello my name is {0} and my age is {1}.'
names_and_ages = [
{
'name':'A',
'age': 18
},
{
'name': 'B',
'age': 19
},
{
'name': 'C',
'age': 17
},
{
'name': 'D',
'age': 21
},
{
'name': 'E',
'age': 22
}
]
for name_and_age in names_and_ages:
print(string.format(name_and_age['name'], name_and_age['age']))
Hello my name is A and my age is 18.
Hello my name is B and my age is 19.
Hello my name is C and my age is 17.
Hello my name is D and my age is 21.
Hello my name is E and my age is 22.
In the example above, the {0} and {1} are called arguments, of the format method . So, anywhere , where the function will see {0} or {1} it will be replaced by the respective variable value.
But , here is a drawback, that , if you have used {0} and {1} , then the first parameter of the format function will always be used by the {0} and similarly the second one will be used by the {1} . But you can change this behaviour , by naming your arguments by yourself.
string = 'Hello my name is {name_to_be_replaced} and my age is {age}.'
names_and_ages = [
{
'name':'A',
'age': 18
},
{
'name': 'B',
'age': 19
},
{
'name': 'C',
'age': 17
},
{
'name': 'D',
'age': 21
},
{
'name': 'E',
'age': 22
}
]
for name_and_age in names_and_ages:
print(string.format(age=name_and_age['age'], name_to_be_replaced=name_and_age['name']))
Hello my name is A and my age is 18.
Hello my name is B and my age is 19.
Hello my name is C and my age is 17.
Hello my name is D and my age is 21.
Hello my name is E and my age is 22.
Here as, we can see , that using this convention , we made normal arguments into keyword arguments. Now , we can pass the values , howsoever we want.
There is one more method which can be used, and you might remember it from the SQL classes, which is the % method .
string = 'Hello my name is %s. And I love %s !'
names_and_loves = [
{
'name':'A',
'love': 'cricket'
},
{
'name': 'B',
'love': 'football'
},
{
'name': 'C',
'love': 'tennis'
},
{
'name': 'D',
'love': 'Badminton'
},
{
'name': 'E',
'love': 'Hockey'
}
]
for name_and_love in names_and_loves:
print(string %(name_and_love['name'], name_and_love['love']))
Hello my name is A. And I love cricket !
Hello my name is B. And I love football !
Hello my name is C. And I love tennis !
Hello my name is D. And I love Badminton !
Hello my name is E. And I love Hockey !
It is working same as concat or format method . It behaves similarly as the argument passed format method . It as also, accepts the first parameter for the first value , and second one for second value and so on.
If you have to incase print the % sign , you would use the %% sign twice.
print('%%s method is same as %s method and %s method .' %('concat', 'format'))
%s method is same as concat method and format method .
Similar to the %s method is the %f method , which prints the float numbers as we want.
print('The 2 decimal places value for PI is %0.2f' %(3))
The 2 decimal places value for PI is 3.00
print('The 2 decimal places value for PI is %0.2f' %(3.14))
The 2 decimal places value for PI is 3.14
print('The 2 decimal places value for PI is %0.2f' %(3.14526162387346872123))
The 2 decimal places value for PI is 3.15
print('The 2 decimal places value for PI is %0.6f' %(3))
The 2 decimal places value for PI is 3.000000
the number after 0 decides the decimal places for the value of the floating point number passed in .
File Manipulation - which is a technique to manipulate files on a system
file = open('README.md', 'r')
content = file.read()
print(content)
file.close()
## Py_tuts
These are basic python tutorials or maybe revision notes for beginners in python programming language. Everyone can access these notes, whenever possible so that ,
no one is deprived from learning programming. Also, These tuts are basic learningpackage for machine learning and deep learning.
names_and_loves = [
{
'name':'A',
'love': 'cricket'
},
{
'name': 'B',
'love': 'football'
},
{
'name': 'C',
'love': 'tennis'
},
{
'name': 'D',
'love': 'Badminton'
},
{
'name': 'E',
'love': 'Hockey'
}
]
file = open('new_file.txt', 'w')
for name_and_love in names_and_loves:
file.write(name_and_love['name'])
file.write('\t')
file.write(name_and_love['love'])
file.write('\n')
file.close()
Here if , you see , using the w method , we will find that , the old content gets lost and only the new content remains. If we do not want this , we should use the a+ method.
file = open('new_file.txt', 'a+')
for name_and_age in names_and_ages:
file.write(name_and_age['name'])
file.write('\t')
file.write(str(name_and_age['age']))
file.write('\n')
file.close()
contents of the file -:
A cricket
B football
C tennis
D Badminton
E Hockey
A 18
B 19
C 17
D 21
E 22
One more thing to note is that , we need to everytime close the file when we open it. This can cause bug , and some resources. To avoid that , python provides us with beautiful method , called with keyword.
with open('new_file.txt', 'r') as file:
print(file.read())
A cricket
B football
C tennis
D Badminton
E Hockey
A 18
B 19
C 17
D 21
E 22
You can use this method , to do any kind of file manipulation, and not worry about closing the file everytime.
Lecture 3
Conditionals and Loops
Conditionals , are used whenever , we want our program to make decisions . Using conditinals like if,else gives the program the ability to make decisions based on the rules we provide it.
For example , if we want our program to only allow users to vote if they are above 18, we use conditionals.
age = int(input('Enter your age :- ')) ## remember , input always returns a string . So keep in mind to convert the integer
#input to integer.
def eligible_to_vote(age):
if age >= 18:
return 'eligible to vote.'
else:
return 'Not eligible'
eligible_to_vote(age)
Enter your age :- 20
'eligible to vote.'
Similarly , we can have as many cases as we want using elif keyword.
Lets say , we want to make a program , to sell candy to only boys below age 10 , sell chocolates to only boys from 11 - 17 and cigarettes to 18 and above .
age = int(input('Enter your age , boy :- '))
def sell(age):
if age <= 10 :
return 'Candy'
elif age >= 11 and age <= 17:
return 'Chocolcates'
else:
return 'Cigarettes'
sell(age)
Enter your age , boy :- 20
'Cigarettes'
Now, we can have as many elif conditions as we want.
Loops are used when we want to iterate over something. They can also be used to make the repetetive tasks smaller and efficient, passing the email to 100 clients or maybe writing something over and over again.
There are 3 main loops , for being the most popular one .
Lets say we want to print tables from 1 to 10 , we can use loops
table = int(input('Enter the number:-'))
def print_table(table):
for x in range(1,11): ## since 11 is not included
print(table * x)
print_table(table)
Enter the number:-10
10
20
30
40
50
60
70
80
90
100
Similarly, we have while loop, which is used when we do not know when the condition is going to end. While loops are most common in game developement, because , a program cannot have the idea , when the user is going to quit, or die.
Lets say we want to make a program to keep taking in the input , till the user keeps saying yes.
def yes(inp):
while inp == 'yes':
inp = input('Enter your input')
yes(input('Enter your input:-'))
Enter your input:-yes
Enter your inputyes
Enter your inputno
There are 2 more important keywords , break and continue, which as the name suggests , the break keyword, breaks the loop and continue keyword, leaves the value at the spot and continues from the next value.
for x in range(1,11):
if x == 5:
break
else:
print(x)
1
2
3
4
for x in range(1,11):
if x == 5:
continue
else:
print(x)
1
2
3
4
6
7
8
9
10
Range() , function can also be used with 3 values/ arguments , that are -
First is the value you want to start from
Second is the value you want to end at
Third is the value , you want to skip to.
for i in range(1,21,2):
print(i)
1
3
5
7
9
11
13
15
17
19
You can also , use loops to iterate over files , or different types of data-structures. Like you can iterate over dictionaries or lists or tuples.
lis = [1,2,3,4,5,6,7,8,9]
dic = {
'name': 'abc',
'age': 19,
'gender': None
}
tup = (12,13,14,15)
for i in lis:
print(i)
for i in dic:
print(i)
for i in tup:
print(i)
1
2
3
4
5
6
7
8
9
name
age
gender
12
13
14
15
In the example above , the loop iterates over the keys of the dictionary and not the values because , you can access the values using the keys .
Lecture 2
Functions
Let’s get a bit mathematical here. A function is anything that takes in an input or a set of inputs , and then provides a single value. Single value is something to keep a note of , because any function that provides more than one output is not considered a pure function.
For example,
f(x) = 4x^2 + 5x + 7
In here , as we can see , that f(x) is a function of x , where all the values which can be inserted into x i.e. input for the functions is called the domain of the function . Like here, all the real numbers R is the domain of the function. Similarly , this funciton , for one particular value of x will provide only one particular output, thus it is a pure function . A pure quaratic function, where the graph of the function would be a parabola on the +ve y-axis , with an intercept of 4.
Now, all the values which the function will produce will be called the range of the function , which in this case is all the +ve numbers till infinity.
The same concept applies into the computer science, term function . It is anything , that takes in a value and then provides a particular value for a given input or a set of inputs. Now the paradigm of programming with only the use of pure functions is called functional programming .
Pure functions are any functions , which -
For any given input provide only one particular output
Are not dependent on the outer scope of the environment context, i.e. the global variables.
Functions are defined using a reserved keyword def , which ironically stands for define, i.e. define a function.
A normal function is defined as -
def name-of-the-function(arguments,if,any): ## your process or code to do some task
def add_ten():
print(12 + 10)
add_ten()
22
if you have not used the return keyword , python will not stop the command from running and keep the function intact.
def add_ten():
print(12 + 10)
print('Hey I am still runnning')
add_ten()
22
Hey I am still runnning
def add_ten():
return 10 + 12
print('Hey I am not running')
add_ten()
22
arguments are just variables which are limited to the functions context. i.e. those variables can only be accessed inside of the function .
age = 18
def eligible_to_vote():
return 10 + age
eligible_to_vote()
28
age = 20
def eligible_to_vote(candidates_age):
print(candidates_age)
return 10 + candidates_age
eligible_to_vote(age)
20
30
print(age)
20
print(candidates_age)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-65-9e7c62b7dc7c> in <module>()
----> 1 print(candidates_age)
NameError: name 'candidates_age' is not defined
If you still have the chull to make the argument globally accessible , you can use the global keyword.
age = 20
def eligible_to_vote(candidates_age):
print(candidates_age)
return 10 + candidates_age
eligible_to_vote(age)
print(age)
print('\n')
print(candidates_age)
20
20
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-66-637dd24445cc> in <module>()
8 print(age)
9 print('\n')
---> 10 print(candidates_age)
NameError: name 'candidates_age' is not defined
age = 20 ## global scope ka var
def eligible_to_vote(candidates_age):
x = 10 ## local scope ka var
return x + candidates_age
eligible_to_vote(age)
print(x)
13
here we are not able to print x since , it is not defined outside of the function, therefore it has a local scope .
age = 20
def eligible_to_vote(candidates_age):
global x
x = 13
return 10 + candidates_age
eligible_to_vote(age)
print(x)
13
A function can take as many arguments as possible. but many of the time, a function is not aware of the number of arguments, thus we use the *arg keyword
def function_with_too_many_arguments(*arg):
print(arg)
function_with_too_many_arguments(12)
(12,)
function_with_too_many_arguments(12,23)
(12, 23)
function_with_too_many_arguments(12,32, 'hello', (12,34), [1,2,3,4,5,6])
(12, 32, 'hello', (12, 34), [1, 2, 3, 4, 5, 6])
Using the *arg keyword , you can pass as many arguments as you wish. Now every argument should not have a mandatory regulation. Thus we use some arguments with default values.
def func_with_default(number):
return number
func_with_default()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-73-7d7b9d0c9c60> in <module>()
----> 1 func_with_default()
TypeError: func_with_default() missing 1 required positional argument: 'number'
This is because , we have called the function without any arguments, which are compulsory to be provided in order to run .
func_with_default(0)
0
def func_with_default(number = 10):
return number
func_with_default()
10
func_with_default(200)
200
This is used, when some argument is not compulsory to be provided by the user. Thus it accepts a default value.
Now , if we have a similar case of unknown arguments , with their keys, then we use the **kwarg keyword , which stands for, keyword argument.
def func_with_default(**kwargs):
print(kwargs)
func_with_default()
{}
func_with_default(arg1 = None)
{'arg1': None}
func_with_default(arg1 = 10, arg2 = 20, arg3 = 30)
{'arg1': 10, 'arg2': 20, 'arg3': 30}
A condition to make a function pure is that , it depends on a single input, and local vars. Thus to make a function pure, we use a method called currying .
Currying is nothing but , a simple method to break many arguments into one single function.
def add_two_values(x,y): ## impure function
return x + y
add_two_values(10,20)
30
## currying used function.
def add_two_values(x):
def second_value(y):
return x + y
return second_value
add_two_values(10)(10)
20
val_one = add_two_values(10)
val_one
<function __main__.add_two_values.<locals>.second_value(y)>
val_one(10)
20
Here, we will see that , the functions are broken into simpler form, using the currying method. First, the add_two_values function takes in a value , and then , stores it in the x var , and then calls the second function, which is defined inside the main function. The local function gets called with the second value passed in and then , it finally returns the added value, the final output. These functions , which call other functions are called high-order-functions or callback-functions .
Lecture 1
Programming
Static vs Dynamic Programming language
Static is a language where , we need to define the content explicitly . cpp -> int age = 18 cpp -> string age = ‘18’
Dynamic is a language where , you do not need to define the content explicitly. python -> age = 18 ; age = ‘18’
Compiled vs Interpreted Languages
Compiled languages are those , which are executed directly by the machine. ex -> cpp
Interpreted langauges are those , which are read and executed line by line. ex-> python
Data types
int -> -ve to +ve values .
string -> ‘absdakahjgdskas dbkjah 18 aksjbdkasjdb !!@(#^!(@#&!(@*#&’
float -> decimal values. -1 -> -1.00
double -> decimal values .
variables
Can be considered as containers or storage boxes which hold any type of value.
eg -> age = 18.00
Constants
Values which remain constant throughout the execution time.
age = 18
age
18
age = 19
age
19
AGE = 18
AGE
18
CONST = 12
def add(val1):
return val1 + CONST
def add_2(val1):
return val1 + CONST
def add_3(val1):
return val1 + CONST
def add_4(val1):
return val1 + CONST
add(12)
24
Keywords
values which cannnot be used as vars or const.
int , str, input, def
Data Structures
Lists/Arrays Dictionaries/Objects Tuples
1) Lists. - A data structure which can hold many value and is mutable.
age = [1,2,3,4,5,6,7,8,9,10, ‘hello’, ‘goodbye’, 1.00, 9.00]
age = ['hello',2,3,4,5,6,7,8,9,10, 'hello', 'goodbye', 1.00, 9.00]
age
['hello', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'hello', 'goodbye', 1.0, 9.0]
age[0]
'hello'
age[1]
2
age[0] = 'hello ashwin'
age[0]
'hello ashwin'
age
['hello ashwin', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'hello', 'goodbye', 1.0, 9.0]
## (0,1) [1,10] [1,10)
age_2 = age[1:10]
age_2
[2, 3, 4, 5, 6, 7, 8, 9, 10]
greetings = age[0]
greetings_2 = age[10:12]
new_greetings = [greetings, greetings_2]
new_greetings
['hello ashwin', ['hello', 'goodbye']]
new_greetings[1][0]
'hello'
len(new_greetings)
2
greet = ['hello ashwin', ['hello', 'goodbye']]
print(greet == new_greetings)
True
greet[0] = 'hello'
new_greetings
['hello ashwin', ['hello', 'goodbye']]
greet
print(greet == new_greetings)
False
Operators
- -> add
- -> subtract
- -> mul
/ -> divide
% -> module/remainder
= -> assignment
== -> comparison
=== -> identical
!= -> not equal
!== -> not identical
1 + 2
3
2 - 1
1
2 / 2
1.0
2 // 2
1
3 / 2
1.5
3 // 2
1
2 * 2
4
3 % 2
1
number = 10.0
number_2 = 10.0
print(number != number_2)
False
Tuples
Same as lists , with only one difference. They are unmutable unlike lists.
tup1 = (1,2)
tup1[0]
1
tup1[1]
2
tup1[0] = 'hello'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-53-64dcc0b3bf40> in <module>()
----> 1 tup1[0] = 'hello'
TypeError: 'tuple' object does not support item assignment
tup2 = (40)
tup2
40
type(tup2)
int
tup2 = (40,'hello', greet)
type(tup2)
tuple
tup2
(40, 'hello', ['hello', ['hello', 'goodbye']])
Dictionary
name_and_age = {'ashwin': 18, 'mehul': 18, 'divyansh_and_hemant': (17,18), 'rest_of_them': [18,18,18]}
name_and_age
{'ashwin': 18,
'mehul': 18,
'divyansh_and_hemant': (17, 18),
'rest_of_them': [18, 18, 18]}
name_and_age['ashwin']
18
name_and_age['ashwin'] = 19
name_and_age
{'ashwin': 19,
'mehul': 18,
'divyansh_and_hemant': (17, 18),
'rest_of_them': [18, 18, 18]}
name_and_age['rest_of_them'][0] = 19
name_and_age['rest_of_them'][0]
19
type(name_and_age)
dict