# What is Python?
• General Purpose High Level Language.
• Interpreted language
• Func onal Program – C language
• Object Oriented Concept - C++ Language.
# History of Python Programming Language
• Developed by Guido Van Rossam in 1989 in Netherland NRI.
• Publically available in 20Feb 1991
• 1969-74 show name = ‘Monty Flying Python Circus’ comedy show.
# Features of Python
• 1) Simple and easy to learn
• 2) Freeware: without paying any cost
• 3) Opensource
• 4) Pla orm Independent
• 5) Portable
• 6) Rich Library
• 7) Extensible- use with c, java
• 8) Embeded- use with combina on in project.
# Applica on of Python
• Web Development
• Game Development
• Internet of Things
• Machine Learning
• Network Program
• Ar ficial Intelligence
• Database
• Google, NASA, ISRO, YAHOO, amazon etc.
# Python Variable: Data or value can be stored in temporary storage spaces
# Datatypes in python
1) integer 2) Decimal/float 3) Boolean 4) String 5) Complex 6) List 7) Tuple 8) Dic onary
9) Set
Sta c Typing:- In C, Java, C++ at the me of variable crea on it required to men on datatype.
Dynamic Typing:- In python at the variable crea on we do not men on datatype.
Dynamic Binding:- In python when you created variable it does not have fixed datatype.
Sta c Binding:- In C once you decided datatype of variable, it cannot change in whole program
# Operators in python
1) Arithme c operators: +, -, *, /(float division), //(integer division), %(modulus), **(power of)
2) Rela onal operators: >, <, >=, <=, ==, !=
3) Logical operators: and, or, not
4) Assignment operators: =
5) Bitwise operators: &, |, ~(bitwise ones' complement), ^(bitwise xor), >>(bitwise right), <<(bitwise le )
6) Membership operators: in, not in
1. Bitwise and operator(&): returns 1 if both bits are 1, otherwise 0
2. Bitwise or operator(|): returns 1 if any of bits is 1. if both bits are 0, then it returns 0.
3. Bitwise xor (^): return 1 if one of the bits is 0 and other bit is 1. if both bits are 0 or 1, then it returns 0.
4. Bitwise Ones' complement operator (~): python ones' complement of number is equal to -(A+1)
5. Bitwise Le shi operator(<<): It shi the le operand bits towards the le side of the given number of mes in
the right operand. in simple terms binary number is appended with 0s at the end.
6. Bitwise Right shi operator(>>): opposite to the le shi operator. Then le side operand bits are moved
towards the right side for the given number of mes. In simple terms, the right side bits are removed.
# Python Tokens: smallest meaningful component in program
1)Keyword 2)Iden fier 3)Literals 4)Operators
1) Python Keywords: process of conversion of high level language to low level language is called compila on. that task
do by compiler and interpreter. certain words are reserved for do this task is called keywords.
interpreted languages - php, javascript, python
False, True, class, finally, is, return, None, con nue, for, lambda, try, def, from, nonlocal, while, and, del, global, not, with,
as, elif, if, or, yield
2) Iden fiers - Iden fier are names used for variables, func ons or objects.
- name in python program is called as Iden fier.
Rules:
1) No special character except _
2) Iden fier are case sensi ve.
3) first le er cannot be digit.
4) space not allowed (only underscore).
5) Reserve words can't use as variable name, class name, func on name.
6) allowed character (alphabets-capital and small).
3) Literals: Raw value or data stored in variable.
literals are constant in python.
Types of literals
1) binary literals 2) decimal literal 3) octal literals 4) hexadecimal literals
5) float literals 6) complex literals 7) string literals 8) boolean literals
9) special literals
# User Input Method
sta c vs dynamic so ware
# Type conversion- process of conver ng one datatype to another.
1) Implicit - interpreter do its own
2) Explicit - programmer request to change type of data
# Flow Control Statements - If else statement
# Flow Control Statement-Looping Statements
# Looping Statements- used to repeat task mul ple mes
eg. song run ll app close, filling bucket with mug of water un l full, salary credited
Types of loops - 1) for loop 2) while loop
while loop - run ll condi on true.
for loop - used to iterate over a sequence (tuple, list, dic onary) act as iterator goes through items that are in sequence.
break, con nue, pass.
Strings - strings are sequence of characters enclosed within single quotes(' '), double quotes(" ") or triple quotes(''' ''').
In python specifically, strings are sequence of unicode characters.
Indexing and slicing
Strings are immutable
Opera ons on string
Func ons:- len, min, max, sorted, upper, lower, capitalize, tle, swapcase, replace, count, find, index, split,
par on, join, startswith, endswith, format, len, isalnum, isalpha, islower, isspace, is tle, isdigit, isiden fier, strip.
# List in python
List is an ordered collec on of elements enclosed within []
List is datatype where you can store mul ple item under 1 name.
more technically, lists act like dynamic arrays which means you can add more items on the fly.
lists are mutable.
Arrays vs List
1. Arrays are fixed size (required to tell how many items store before crea on like int[50]) and lists are dynamic size
2. Convenience: array are homogeneous and lists are heterogeneous.
3. Speed of execu on of list is slower than array.
4. Memory: List occupies more space compared to array.
Referen al array: store address of list items due to referen al array we store different datatype in list.
characteris cs of lists
1. Ordered
2. changeable/Mutable
3. Heterogeneous
4. can have duplicates
5. are dynamic
6. items can be accessed
7. can contain any kind of objects in python.
Indexing and slicing
Func ons: append, extend, insert, pop, remove, clear, sort, len, max, min, sorted, count, index
Opera ons on list
# list comprehension
List comprehension provides a concise way of crea ng lists
newlist=[expression for item in iterable if condi on==True]
Advantages of List Comprehension
1. More me efficient and space efficient than loops
2. Require fewer lines of code
3. Transforms itera ve statements into a formula
# Zip
The zip() func on returns a zip object, which is an iterator of tuples where the first item in each passed iterator is
paired together and then second items in each passed iterator are paired together. If the passed iterators have
different lengths, the iterator with least items decides the length of the new iterator.
Disadvantages of python list
1) slow 2) Risky usage 3) eats up more memory
# Tuple
Tuple is an ordered collec on of elements enclosed within ( )
tuples are immutable
Characteris cs
Ordered
Unchangeable
Allows duplicates
Opera ons on tuple
Func ons: len, sum, min, max, sorted, count, index
Difference bet list and tuple
1. Syntax
2. Mutability
3. Speed - tuple are faster than list.
4. Memory - tuple occupies less space compared to list
5. Built in func onality - more in list compared to tuple
6. Error prone - list more error prone compared to tuple
7. Usability
# Dic onary
Dic onary is an collec on of key-value pairs enclosed with { }
used to store data values like a map, which unlike other data types which hold only a single value as a datatype
In some languages it is known as map or associa ve array.
Dic onary is mutable
Characteris cs
1. Mutable
2. Indexing has no meaning
3. keys can't be duplicated
4. keys can't be mutable items
Func ons: keys, values, items, update, pop, popitem, clear, len, sorted, min, max
# Dic onary comprehension
{key:value for vars in iterable}
# Set
Set is an unordered and unindexed collec on of elements/items enclosed with { }
Duplicates are not allowed in set i.e. set elements are unique() and must be immutable. however, a set itself is
mutable. we can add or remove items from it.
Characteris cs
1. Unordered
2. Mutable
3. No Duplicates
4. Can't contain mutable data type
Func ons: add, update, discard, remove, pop, clear, union, intersec on, difference, symmetric_difference,
intersec on_update, difference_update, symmetric_difference_update, len, sum, min, max, sorted, isdisjoint,
issubset, issuperset
# frozenset: It is just an immutable version of a python set objects.
what works and what does not
works -> all read func ons
doesn't works -> write opera ons
# set comprehension
# Python Func ons
A func on can be defined as organized block of reusable code, which can be called whenever required.
Two principles are used in func ons
Abstrac on
Decomposi on
Types of func ons
1) In-Built - predefined in python
2) User Defined - defined by user to perform specific task
3) Anonymous
Advantages of func on
1) avoid rewri ng the same logic/code again and again in a program
2) we can call python func ons mul ple mes in a program and anywhere in a program
3) can track large python program easily when it is divided into mul ple func ons
4) Reusability is the main achievement of python func on
User Defined func ons
1) func on defini on
2) func on calling
return statement
It is used at the end of func on and returns the result of the func on.
It terminates the func on execu on and transfers the result where the func on is called.
return statement cannot be used outside of the func on
If return statement has no expression or does not exist itself in the func on then it returns None object.
Arguments in Func on:
type of informa on which passed into func on
arguments specified in parenthesis
pass any number of arguments separate them with comma.
Types of Arguments
1) Required arguments/Posi onal arguments
2) Default arguments
3) Variable length arguments
4) Keyworded arguments
1) Required Arguments/Posi onal Arguments
which are required to be passed at the me of func on calling with the exact match in their posi ons in the
func on call and func on defini on.
If the either of the arguments is not provided in the func on call, or the posi on of the arguments is changed,
the python interpreter will show the error.
2) Default Arguments
Python allows us to ini alize the arguments at the func on defini on.
If the value of any of the arguments is not provided at the me of func on call, then that argument can be
ini alized with the value given in the defini on even if the argument is not specified at the func on call.
3) Keyworded Arguments
python allows us to call the func on with the keyword arguments.
This kind of func on call will enable us to pass the arguments in the random order.
The name of the arguments is treated as the keywords and matched in the func on calling and defini on.
If the same match is found, the values of the arguments are copied in the func on defini on.
python allows us to provide mix of the required arguments and keyworded arguments at the me of func on
call, However, the required arguments must not be given a er the keyworded argument, i.e. once the keyword
argument is encountered in the func on call, the following arguments must also be the keyworded arguments.
4) Variable Length Arguments
Large projects, some mes may not know the number of arguments to be passed in advance. In such cases,
python provides us the flexibility to offer the comma-separated values which are internally treated as tuples at
the func on call
by using this arguments we can pass any number of arguments
*args and *kwargs are special keywords that are used to pass variable length of arguments to the func on
*args allows us to pass variable number of non-keyworded argument to func on.
**kwargs allows us to pass any number of keyworded arguments.
keyworded arguments means that they contain a key-value pair, like a python dic onary.
Scope of variables
1) Global Variable 2) Local Variable
scope of variable depends upon the loca on where the variable is being declared
variable declared in one part of the program may not be accessible to the other parts.
1) Local variable: variable defined inside func on
2) Global variable: variable defined outside the func on
First class ci zen
-In programming language design, a first-class ci zen (also type, object, en ty, or value) in a given programming language
is an en ty which supports all the opera ons generally available to other en es. These opera ons typically include
being passed as an argument, returned from a func on, and assigned to a variable.
- func ons are 1st class ci zen
- func ons are immutable
Benefits of using a func on
code modularity
code readability
code reusability
Lambda Func on
It is small anonymous func on
can take any number of arguments but can have only one expression
Lambda keyword : create lambda expression
Parameters: One or more parameters are supported. Must be separated by comma(,) and no parenthesis
colon: This is cue for expression
Expression: Must be single valid python expression
eg. lambda a,b: a+b
Difference bet lambda and normal func on
1. No Name
2. Lambda has no return value (infact, return a func on)
3. lambda is wri en in one line
4. not reusable
lambda func ons are used with Higher Ordered Func on(HOF)
Higher Order Func ons
those func on return func on. or those func on receive other func on as input.
three HOF -> 1) Map 2) Filter 3) Reduce