Python Book
Python Book
To truly understand this book by heart and make the most out of
channel: Sheryians AI
Downloading Python
Downloading IDE
Comments in python
Comments are something that are ignored by the python
interpreter
We have to use # for writing a comment in python
Multiline comments are not available in python but we can
achieve it by using Doc String “”” Multiline ”””
Variables in python
Age = 12
Numbers
Integer - All the numbers excluding decimal places and fraction.
Float - All the decimal numbers and fraction values are Float.
Strings
Strings - This is used to store anything in python, literally anything
Boolean
Boolean - Theres nothing much to say this is the data type which
Type conversion
4 things
There are more functions like this but these are 4 main
function, looking at these functions you can guess these are
a = str(a)
Implicit Explicit
type to another
type to another.
You have seen the example at
previous page.
example before
int() -
Integer
eg - a = 12
float() -
Float
print(a/2)
complex() Complex
output - 6. -
str() String
list() List
tuple() Tuple
set() Set
dict() -
Dictionary
bool() - Boolean
s h e r y i a n s c o d i n g s c h o o l
Type conversion concepts
see.
There are truthy values and Falsy values, and there are only
0.0
False
“”
[]
{}
Output
You probably know till now, how to provide the output of the
code you have written and that is with print() function
There is no other functions to provide the result on the
terminal we just have to use print() function
Now when providing the output we can use variables in print
statement using a formatted string as shown in the below
example.
Input
But the main question is how to ask user for some
information.
For example there is a user and you want to ask the age of
that user, how can you do so, it’s easy using input()
Now the default data type of input is always string reason is
simple you can store anything in string.
You have to manually convert the data type of input
statements.
Questions
**Important** watch full video for clear understanding
Accept numbers from a user
Accept age from the user and print it.
Arithmetic operators
Arithmetic operators perform mathematical operations like
addition, subtraction, multiplication, division, etc
There are 7 types of arithmetic operator.
addition -
subtraction -
-
multiplication -
division -
Floor division -
/
-
modulus -
Exponentiation - *
Eg - a = 12
b = 8
print(a+b)
Output - 2
See the video for proper explanation.
etc
works better
*= -
Multiply and assign
/= -
Divide and assig
-
s h e r y i a n s c o d i n g s c h o o l
Comparison operator
> -
Greater than
< -
Less than
>= -
Greater than or equal t
-
<= -
Less than or equal t
Logical operators
Conditional statements
Conditional statements in Python allow decision-making by
executing different blocks of code based on conditions
Decision making can be understood with an example
will do task B.
Number
Task A Task B
Q1. Accept two numbers and print the greatest between them.
Q2. Accept the gender from the user as char and print the
or odd.
Q4. Accept name and age from the user. Check if the user is a
Loops in python
Manually printing will take 100 code lines to print it. but using
power of loops.
Types of loops
s h e r y i a n s c o d i n g s c h o o l
In scenario 1 you have to transfer 4 mugs of water from 1
bucket to another
In scenario 2 you have to transfer all the water from 1st
bucket to another via mug.
Intuition of loops
In first scenario you know the number of mugs to transfer
from one bucket to another.
Here you know how many numbers of iteration you have
to go through, like you have to transfer only 4 mugs.
So when you know the number of iterations you will use a
FOR loop.
In the second scenario, you don't know how many mugs you
need to transfer, but you do know the condition that
determines when to stop
So when you don’t know how many iteration you have to
use but you know a condition that determines when to
stop you will use WHILE loop.
Range function
function
So this is how you use a for loop. watch the full video for
clear understanding of syntax.
Here you iterated over the string using the index values for
better understanding watch the video
statement.
the iterations will run for understanding lets say you will not
run the 16th lap but all other lap will be there.
You have seen the else statement used with if, but it can also
s h e r y i a n s c o d i n g s c h o o l
For Loop questions
Ex - 6 = 1, 2, 3 =
Check wether the number is prime or not
Expected Outcome:
Chars = 8
Digits = 3
Symbol = 4
While loop
len() etc
But you can create your own function and they are called as
user defined functions. To make your own function you have
to use def keyword and then name the function. After this
you have to call the function using name() and paranthesis.
that are working like variables then we can pass the values to
As you can see name is the parameter and Alice is the argument
that we passed to name. And you can pass N number of
parameters and arguments but they must be same like if you
have taken 3 parameters you have to provide 3 arguments
otherwise there will be error.
And another thing is first parameter, will always capture first
argument and so on. These arguments are called positional
argument.
Lets be clear this python notes are not for the DSA this will
cover all the in-build data structures.
List Powers
List Basics
Tuple Powers
Yes there are only 2 methods of tuple one for finding the
index and other of counting the occurrences of an element.
Set Powers
Set methods
Now set methods are very powerful cause you don’t have
any indexing you cannot change the values but set is
mutable so we use methods for this
For adding and removing the elements you can use methods
as follows
Now these are some of the basic methods but sets also have
some special methods for performing some special
operations between 2 sets.
Dictionary Powers
Dictionary methods
There are not many dictionary methods lets see the working
of some
as you all know we can use help(dict) for getting the
information of all the methods available.
Dictionary Questions
Write a Python script to merge two Python dictionaries
Write a Python program to sum all the values in a dictionary
Count the frequency of each element
Write a Python program to combine two dictionary by adding
values for common keys.
Errors
Syntax error
Indentation Error
Exceptions
Keyword Purpose
You all know what are files any name with an extension is
file
Now that extension can be .py , .txt , .mp3 etc. and when we
want to handle these files we will use file handling.
File handling
File handling means Creating, Reading, Updating,
Deleting(CRUD) operations that we can perform in files
Now lets see how to perform these operations in python.
We have to use open() function to open a file in python.
Mode Description
You all know what are files any name with an extension is
file
Now that extension can be .py , .txt , .mp3 etc. and when we
want to handle these files we will use file handling.
File handling
File handling means Creating, Reading, Updating,
Deleting(CRUD) operations that we can perform in files
Now lets see how to perform these operations in python.
We have to use open() function to open a file in python.
Mode Description
What is OOPS
Classes
Syntax of class
Objects
BAG
FACTORY
REQUIREMENT
Material Pockets
Zips
What is constructor
Types of Attribute
instance attribute.
Types of Methods
parameter.
class.
Inheritance
Property
Now the inherited class has all the powers of parent class that
means all the methods, attributes can be accessed by the
instance of child class as well.
Constructor in Inheritance
Types of Inheritance
Single Inheritance
All the inheritance we saw above was single level.
Multiple Inheritance
Multiple Inheritance means there will be 2 parent classes and
only 1 child class and the child class will inherit all the
attributes and methods of both parents.
Note - The constructor function will be inherited of the first
class that has been Inherited. This is MRO(Method Resolution
Order) followed by python.
Polymorphism
Types of Polymorphism
Method Overriding
This is where a child class overrides a method of the parent
class, and Python decides at runtime which method to call,
based on the object type.
Encapsulation
Abstraction
Now there are various dunder method see the video for some
examples.
Now lets create a Bank management project using Oops in
python.
Advance questions
This portion is only completed in video and here you will only see
the Question so please watch the video to complete it.
And you are good to go If you have completed till now but this
section is for those who wants advance question solving skills
Some patterns first.
Right angle triangle with stars.
mirrored right angle triangle with stars.
A triangle with stars.
A diamond with stars
Question 1 - Strong number.
Question 2 - Prime numbers in a Range.
Question 3 - Find which element occurred most in a List
Decorator
Lambda functions
Module is just a single file containing code and we can use this
file code in other file
A single Python file (.py
Contains functions, variables, or classe
Used to organize and reuse code
Python comes with lots of ready-to-use modules like
math (for math operations
random (for generating random numbers
datetime (for date and time)