Python
Python
</>
“ Python is an experiment in how much freedom
programmers need. Too much freedom and nobody can
read another's code; too little and expressiveness
is endangered
”
— Guido van Rossum
Key Features
● Object-oriented Scripting
● General-purpose Interpreted
● Extensible feature
Many more…..
Applications of Python
● Web Development
● Data Science
● Enterprise Applications
● Education Sector
● Game Development
https://www.python.org/downloads/
● Can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )
n = 3 type(n)
m = ”John”
● Object Identity
● Casting variables
id(a)
x = str(3)
y = int(3)
z = float(3)
Reserved Words (Keywords)
class from or
● Bitwise Operators
● Logical Operators a. & AND
a. not b. | OR
b. and c. ^ XOR
c. or d. ~ NOT
e. << Zero fill left shift
● Identity Operators f. >> Signed right shift
a. is
b. is not
Conditional Statements
It allows conditional execution of a statement
or group of statements based on the value of an
expression.
The conditions are evaluated and processed as true or false. If this is found to be true, the program is run as
needed. If the condition is found to be false, the statement following the If condition is executed.
1. If the statement
2. If else statement
3. Nested if statement
4. If…Elif ladder
5. Short-Hand if statement
A while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And
when the condition becomes false, the line immediately after the loop in the program is executed.
Syntax:
while expression:
statement(s)
For Loop
For loop is used for sequential traversal i.e. it is used for iterating over an iterable like String, Tuple, List, Set
or Dictionary.
Syntax:
# statements
Command Line Arguments
The arguments that are given after the name of the
program in the command line shell of the operating
system are known as Command Line Arguments.
Sys Module
● Another way to accept input function other than input()
● Inputs will be passed to the program as an argument from the command prompt
● Sys module is used to process the inputs form the command prompt
● All the arguments passing to the command prompt will forms a List as argv
● List items are ordered, changeable or mutable, and allow duplicate values.
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
● A tuple is a collection which is ordered and unchangeable or immutable. Also allow duplicate
values
● A tuple can contain different data types like string, integer, float, boolean etc.
Sets
● A set is a collection which is unordered, unchangeable*, unindexed and do not allow duplicate
values.
● Unordered means that the items in a set do not have a defined order. Set items can appear in a
different order every time you use them, and cannot be referred to by index or key.
● Once a set is created, you cannot change its items, but you can remove items and add new items
items() Returns a list containing a tuple for each key value pair
setdefault() Returns the value of the specified key. If the key does not exist: insert the
key, with the specified value
update() Updates the dictionary with the specified key-value pairs
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
Delete a file/ folder
● To delete a file, you must import the os module, and run its os.remove() function
● To delete a folder, import the os module, and run its os.rmdir() function
Functions
● Major use of function is its Reusability. Once we write a function, that function can be called number
● In order to re-write a logic, we can write a single generalised function and can make use of it.
● Debugging the program will be easier for the user. If a program with more line of code or more
complex logic has to be executed, it would be difficult to find the errors(especially the logical errors).
● Function Declaration
● Function Call
● Function Definition
In python, first start write the function definition, then write the call function inside the program.
Function Definition
● Parameters/ Arguments are the inputs given to the user defined function, which is optional
● Every function name with / without arguments should end with colon
● Return can be empty or value. Every function will return its control to its calling function
● Multi-value return can be accepted. In python, function can return multiple values to the
● Function name
Equal to the Function definition
● Arguments / Parameters
Same function name and the same number of arguments/parameters should be returned in the
Function definition.
● These variables can be accessed from both inside and outside of the function.
● Scope of local variable is only within the function, where the variables have been declared
● If local and global variables have same value, first preference is for local variables.
Variable scope and Return Values
Return function will give the value and store it in a variable and can be use in the later programs
On the other hand, the print can only display the values, it never store the values
Lambda Functions
● A lambda function is an anonymous function.
● Function doesn’t hold any name, but the lambda function can be assigned to any variable.
Syntax:
learning, data science, data visualization, image and data manipulation applications, and more.
● There are built-in functions or methods, which will reduce the code written by the user.
❖ Pygames : Writing Video games, consist of computer graphics and sound waves
❖ Scikit : ML and statistical modeling for classification, regression, clustering via consist interface
To install a python package or module, enter the code below in Anaconda Prompt
● Search for folder named Scripts where pip applications are stored.
To install a python package or module, enter the code below in Anaconda Prompt
!pip install package-name #some users face error "SyntaxError: invalid syntax"
OR
Description Command
Object-oriented programming (OOPs) is a computer programming model that organizes software design
OOPs Terminologies
● Class
● Object
● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
Classes
● Every class can have multiple objects/ instances, classes are always followed by object
● These are a user-defined data type that is used to encapsulate data and associated functions together.
Class
Attributes Data
Class Members
Function Function
Objects
● With the help of objects, we can access the methods and attributes of a class.
● All the attributes and functions created for the class are available for the objects as well.
Class Object
Student Student1
Roll No Roll No
Name Name
read() Branch
write() read()
write()
Abstraction
● An abstract method is a method that has a declaration but does not have an implementation
● A class which contains one or more abstract methods is called an abstract class.
Encapsulation
○ Public : Data & function from one class can be accessed by any function in any class
○ Protected : Data & function are only accessed by the class where it is declared and inherited
○ Private : Data & function are only accessed by the class where it is declared only.
● Major concept is base class and its derivatives. A derived class can inherit the properties of a base
class.
● Derived class will be having same attributes and functions of the base class and also it can have another
attributes and functions which are not present in the Parent Class.
● Whatever attributes and functions are declared in the parent class, those properties can be accessed and
● Inheritance can be implemented in different ways, Single Inheritance, Multiple Inheritance, Multi-level
Inheritance.
Polymorphism
Class Syntax:
●
Class & Objects
● Logical error
○ missing (:)
● The code will be syntactically correct and the code compiles correctly, but the output will be
wrong
● These errors can be figured out at least at the time of quality checking of the code.
● The machine understands what you are saying, but runs into trouble when following the
instructions.
● Syntactically and logically correct, but if the user is not following the instructions correctly
Thank you!!!