Logical operators
Conditional Expressions
Conditional expression are a shortcut for if-else statements also
called as ternary operators
If a value is violating a rule and fulfilling multiple condtional
statements then only one will get printed. The conditional statement
present in prior will get printed
STRING METHODS
A string is a series of character
String indexing
Indexing means accessing elements of a sequence using [] also called
as the indexing operator
[start:end:step] is the sequence followed in the indexing operator
Format specifiers
While loop
For loop – executes a block of code a fixed number of times
13 is an unlucky number. We do not want to print it. So we will use
the continue function
Use break keyword to stop printing anything at all after 12
Nested loops
Set, Tuples and List
THE SHOPPING CART PROGRAM
EXPLANATION OF HOW DID IT PRINT IN A PROPER BILL FORMAT
2d collections [2d lists, tuples and sets]
Till now we had studied 1d collections. Now we will study 2d
collections.
Talking aout 2d lists, these are nothing but lists inside lists.
Another format to write and print 2d lists
QUIZ GAME
Some extra data about lists, tuples ans sets
Random Module
Functions
A function is a block of reusable code. We can invoke the function by
placing a () parantheses in front of that function
Remember the order of arguments is very important while invoking a
function
Till now we have studied positional arguments in functions. Now we
will study default, arbitrary and keyword arguments
Default Arguments
1. If a function contains more than one argument in the
parentheses. And while invoking a function one or multiple
arguments remain the same.
2. Then while invoking a function you don’t need to again and
again put the constant argument everytime you invoke
3. For constant arguments. Put a constant value by default within
the function itself
4. As shown below. If I buy a PS5, discount and tax is going to be
the same for very unit. So put it default
5. Even if you put default. You can still change the value by
putting the value while invoking a function as shown above
A default argument should never come before the nondefault
argument. You must write the end argument first and then the start
argument. Don’t worry it will still work the same.
Keyword arguments
We know that the position of an argument is very important. What if
we don’t remember the position of an argument when we are
invoking a function, that is when keyword arguments are used
Also make sure that
Positional arguments must be first as compared to keyword
arguments
Arbitrary arguments
Arbitrary arguments mean a varying number of arguments. We
don’t know how many arguments are going to be input by the
user
We use *args – arguments
And **kwargs – keyword arguments
* is the unpacking operator here
When args are used as the parameters, unpacking operator
unpacks them into tuples
When kwargs are used as the parameter, the unpacking
operator unpacks them into dictionaries
For example if we create a function of additoin that takes 2 inut
numbers, but we type 3, this gives us an error. To tackle this
error:
All we need to do is iterate it with a loop so that it adds all the
numbers that weere given while invoking a function
Naming it “arg” in the parantheses is nt necessary but an asterisk
is. You can name it anything
A similar thing can be done with keyword arguments but we need
double asterisk for the unpacking operator
Below is a case where *args and **kwargs both are present.
Please make sure that args has higher priority as compared to
kwargs. Also remember that kwargs need to be handled like a
dictionary
Iterables
List,tuples,sets and dictionaries all are iterables
Membership Operators
Used to check whether a value or variable is present in a sequence or
not (sequence may be a list, set, tuple or dictionary)
Keywords used are
1. In
2. Not in
Below is a simple program to get grades of students
List comprehension
It is a concise way to create a list in python which is easier as
compared to the traditional loops
Match case statements
This uses case instead of if-else statements. With help of this, the
code looks a lot cleaner
Module – it is a file that contains already written code that you can
use in your program by th help of import function
You can take the help of – print(help(module)) to know all the
modules of python
To know all the functions of a module just write print(help(math))
You can also write different code pages and import them into one
Variable Scope
Tells us about how much a variable is visible and accessible
Currently it will print x = 2. But if I delete x = 2 then it will move
It will move towards the enclosed variable x and now will print 1
Slot machine game
Code encryption program
Hangman game mini project
OBJECT ORIENTED PROGRAMMING
An object is a bunch of related attributes (just like variables) and
methods (just like functions)
To create an object --- you need help of a class
Class is a type of blueprint that are used to desgin the structure and
layout of an object
Methods are actions that an object can perform
Class variables and instance variables
An example of class variables
An example of using a class variable inside an instance constructor
INHERITANCE
It allows a class to inherit methods and attributes from anoother
class. Helps with reusability and extensability
Given by the word – class Child(Parent)
Lets add more instance variables in the child classes
Multiple and multilevel inheritance
Multiple inheritance says that a child class can inherit from more
than one parent class that is multiple.
Whenever inheritance takes place on more than one level it is called
as multilevel inheritance as shown below
If we want to add a name to them, do so by __init__ constructor and
then inside that constructor, put [Link] = name
Super() function
It is a function used in CHILD CLASS [subclass] to call methods from
PARENT CLASS[superclass]
Polymorphism – it means to have many forms
So, according to polymorphism, pizza is a pizza, pizza is also a circle,
pizza is also a shape. This means it has many forms. Just ignore the
list parts its not that much important
Static methods, instance methods and the difference between them
Static methods do not compulsarily need an object to be created.
They can be acessed with help of class, without needing to create an
object.
Class methods
Just like for instance methods the first attribute is self. In a similar
manner, for the class method, the first attribute to acess the method
is “cls”
Decorators
A decorator cannot handle argumented functions on its own that’s
why it requires the help of *args and **kwargs
Exception Handling in Python
In Python file handling, there are two types of file paths:
Relative file path – The file is located in the same folder as my Python program or inside my
project folder, so I only give the file name or short path.
Absolute file path – The file may be located anywhere on the computer, so I give the full
address of the file.
The output it gives us is
Below is the file handling for json format files
Below is the file handling for csv files (Comma Separated Value)
It gives us the following output
Reading Files in python
The file was already created beforehand and then the filepath of that particular file was
input here
Similarly the following is done for the json files as well
Similarly the following is done for the csv files as well
Multithreading in python
This will take ime ecause of the time module. These are completed in a sequence one by
one this is because all of them run on the same thread
However the below threading method can be applied
The sequence has been changes because of this. Now all three run on different threads
We need to print the statement only after all of the chores are complete, and that is why we
use the join function
Below is an example of how you can put arguments as well within the multithreading
Encapsulation in Python