What Is Python
What Is Python
What Is Python
Features of Python:
Installation
Create a file called hello.py and paste the below code into it
Modules
A module is a file containing code written by somebody else
(usually) which can be imported and used in our programs.
Pip
E.g., pip install flask (It will install flask module in your
system)
Types of modules
Comments
Types of Comments:
There are two types of comments in python,
a=30
b=”Harry”
c=71.22
Copy
Variable – Container to store a value
1. Integers
2. Floating point numbers
3. Strings
4. Booleans
5. None
a = 71 #Identifies a as class<int>
Operators in Python
a = 31
type(a) #class<int>
b = “31”
type(b) #class<str>
Copy
A number can be converted into a string and vice versa (if
possible)
input() function
This function allows the user to take input from the
keyboard as a string.
Chapter 3 – Strings
word = “amazing”
word = ‘amazing’
String Functions
len(‘harry’) #Returns 5
Copy
<|DATE|>
Copy
List Indexing
L1 = [7, 9, ‘harry’]
L1[0] – 7
L1[1] – 9
L1[70] – Error
List Methods
Tuples in Python:
A tuple is an immutable (can’t change or modified) data type
in Python.
Tuple methods:
a = (1, 7, 2)
Copy
a = (7, 0, 8, 0, 0, 9)
Copy
Syntax:
1. It is unordered
2. It is mutable
3. It is indexed
4. It cannot contain duplicate keys
Dictionary Methods
a = {“name”: “Harry”,
“from”: “India”,
“marks”: [92,98,96]}
Copy
Sets in Python
S.add(1)
S.add(2)
# or Set = {1,2}
Copy
If you are a programming beginner without much knowledge
of mathematical operations on sets, you can simply look at
sets in python as data types containing unique values.
Properties of Sets
Operations on Sets
S = {1,8,2,3}
Copy
S = Set()
S.add(20)
S.add(20.0)
S.add(“20”)
Copy
What will be the length of S after the above operations?
Syntax:
'''
if (condition1): // if condition 1 is true
print(“yes”)
elif (condition2): // if condition 2 is true
print(“No”)
else: // otherwise
print(“May be”)
'''
Copy
Code example:
a = 22
if (a>9):
print(“Greater”)
else:
print(“lesser”)
Copy
Quick Quiz: Write a program to print yes when the age
entered by the user is greater than or equal to 18.
Relational Operators
= = -> equals
Logical Operators
elif clause
'''
if (condition1):
#code
elif (condition 2):
#code
elif (condition 2):
#code
….
else:
#code '''
Copy
90- E
100 x
80-90 A
70-80 B
60-70 C
50-60 D
<50 F
1. While loop
2. For loop
While loop
An Example:
i = 0
while i<5:
print(“Harry”)
i = i+1
Copy
(Above program will print Harry 5 times)
For loop
l = [1, 7, 8]
for item in l:
print(item)
Copy
(Above program will print 1, 7, and 8)
Example:
l = [1, 7, 8]
for item in l:
print(item)
else:
print(“Done”) #This is printed when the loop exhausts!
Copy
Output:
Done
Copy
Example:
Example:
for i in range(4):
print(“printing”)
if i == 2: #if i is 2, the iteration is skipped
continue
print(i)
Copy
pass statement
Example:
l = [1, 7, 8]
for item in l:
pass #without pass, the program will throw an error
Copy
***
**
*** for n = 3
Copy
* * *
* * #For n=3
* * *
Copy
10. Write a program to print the multiplication table of n
using for loop in reversed order.
def func1():
print(“Hello”)
Copy
This function can be called any number of times, anywhere
in the program.
Function call
def greet(name):
gr = “Hello” + name
return gr
Copy
a = greet(“Harry”) #“Harry” is passed to greet in name
def greet(name=’stranger’):
#function body
Copy
greet() #Name will be ‘stranger’ in function
body(default)
Recursion
factorial(n) = n * factorial(n-1)
Copy
This function can be defined as follows:
def factorial(n):
if i == 0 or i == 1 : #Base condition which doesn’t call the
function any further
return i
else:
return n*factorial(n-1) #Function calling itself
Copy
This works as follows:
The programmer needs to be extremely careful while
working with recursion to ensure that the function doesn’t
infinitely keep calling itself.
***
** #For n = 3
*
Copy
Opening a file
open(“this.txt”, “r”)
Copy
Here, “this” is the file name and “r” is the mode of opening
(read mode)
f.close()
Copy
With statement
The best way to open and close the file automatically is the
“with” statement.
with open(“this.txt”) as f:
f.read()
Copy
#There is no need to write f.close() as it is done
automatically
Class
Object
Class Attributes
Example:
Class Employee:
company = “Google” #Specific to each class
harry = Employee() #Object instantiation
harry.company
Employee.company = “YouTube” #changing class attribute
Copy
Instance Attributes
harry.name = “Harry”
harry.salary = “30K” #Adding instance attributes
Copy
Note: Instance attributes take preference over class
attributes during assignment and retrieval.
harry.attribute1 :
‘self’ parameter
harry.getSalary()
Copy
here, self is harry, and the above line of code is equivalent
to Employee.getSalary(harry)
class Employee:
company = “Google”
def getSalary(self):
print(“Salary is not there”)
Copy
Static method
For Example:
class Employee:
def __init__(self,name):
self.name = name
def getSalary(self):
#Some code…
harry = Employee(“Harry”) #Object can be instantiated using
constructor like this!
Copy
Syntax:
Type of Inheritance
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
@classmethod
def (cls, p1, p2):
#code
Copy
@property decorators
class Employee:
@property
def name(self):
return self.ename
Copy
if e = Employee() is an object of class employee, we can
print (e.name) top print the ename/call name() function.
@name.setter
def name(self, value):
self.ename = value
Copy
Operator overloading in Python
p1 + p2 -> p1.__add__(p2)
p1 – p2 -> p1.__sub__(p2)
p1 * p2 -> p1.__mul__(p2)
p1 / p2 -> p1.__truediv__(p2)
p1 // p2 -> p1.__floordiv__(p2)
Copy
Other dunder/magic methods in Python
__str__() -> used to set what gets displayed upon calling str(obj)
__len__() -> used to set what gets displayed upon calling .__len__()
or len(obj)
Copy
Chapter 11 – Practice Set
7i + 8j + 10k
Copy
Assume vector of dimension 3 for this problem.