My Notes
My Notes
History of python:
The first public version, Python 0.9.0, was released in February 1991, and it was the first to
offer functions, modules, exception handling, and dynamic typing - making it possible for
users to program what they wanted with ease.
Why is Python built?
Whitespace is Important:
Python lets its users define indentation as a part of the syntax; this helps
improve the organization of code and is more readable.
High-Level Language:
Python is designed to be easy to read and write, using English-like keywords
and a simple syntax.
Dynamic Typing:
Python does not require explicit type declarations; the type of a variable is
determined at runtime.
Object-Oriented Programming:
Python supports object-oriented programming concepts like classes, objects,
inheritance, and polymorphism.
Open Source and Free:
Python is free to use and distribute, and its open-source nature fosters a large
and active community.
Cross-Platform:
Python code can run on various operating systems (Windows, macOS, Linux)
without modification.
Large Standard Library:
Python comes with a comprehensive standard library that provides a wide
range of modules and functions for common tasks.
Large Community Support:
The Python community is large and active, providing ample resources,
tutorials, and support for developers.
versatile and Flexible:
Python can be used for a wide range of applications, including web
development, data analysis, machine learning, scripting, and more.
Readability:
Python emphasizes code readability, using whitespace for formatting and
English keywords, making it easier to understand and maintain.
Frameworks:
frameworks for various tasks, such as web development (Django, Flask), data
science (NumPy, Pandas, Scikit-learn), and more.
Applications of Python
1. Web Development
Python is widely used in web development. It makes use of simple code to build a beautiful
and functional website.
Many AI and ML models are built using Python. Data analysts use it too. The fundamental
cause for this is the availability of many tools and libraries specific to these applications.
3. Game Development
One of the interesting applications of the Python Programming language is to build games
like egg catcher, snake game, etc.
Its simplicity and platform independence nature make it a good choice for GUI applications.
5.Software Development
Python supports the development of software with its libraries and packages. It is used for
building, testing, management, and so on.
6. Educational Purposes
Python helps beginners get introduced to the Programming environment. It is because of its
simple syntax and beginner-friendly nature.
Many operating systems are using Python as a backbone. Generally, it is used along with C.
8. Language Development
Yes! Python being one of the programming languages helps in building other languages.
These have similar syntax object models and indentation as compared to Python.
9. Web Scraping
Nowadays, Python is being widely used for web scraping purposes. We can access the huge
data over the internet using these web scraping techniques.
Data Science and Data Analysis are two of the booming fields in the market. This involves
analysis of a huge amount of data, finding relations and doing future predictions.
FRAMEWORKS :
Pandas for Data Manipulation and Analysis.
Variables
Variables are containers for storing data values.
Variable names can contain letters, numbers, and underscores.
They must start with a letter or an underscore.
Variable names are case-sensitive (e.g., age, Age, and AGE are distinct variables).
Avoid using Python's reserved keywords as variable names.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type, and can even change type
after they have been set.
Example
x=5
y = "John"
print(x)
print(y)
Example
a=4
A = "Sally"
A,b,c=1,”sri”,4.5
Print(a)
Print(b)
Print(c)
Type Casting is the process of converting the value of one data type into another. In some
cases, Python automatically converts types. However, there are few built-in functions
like int(), float(), str() and more.
explicit type conversion, also known as type casting, is done by the programmer using built-
in functions.
Example
# type casting
a = 10 # int
b=a/3
print(b)
b= int(b)
print(b)
output:
3.33
Keywords
Python has a set of keywords that are reserved words that cannot be used as variable
names, function names, or any other identifiers:
Keyword Description
finally Used with exceptions, a block of code that will be executed no matter if
there is an exception or not
Operators:
Operators are the symbols used to perform a specific operation on different
values and variables. These values and variables are considered as
the Operands, on which the operator is applied. Operators serve as the
foundation upon which logic is constructed in a program in a particular
programming language.
Different types of Operators used in Python are as follows:
1. Arithmetic Operators
2. Comparison Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
S. No. Operator Syntax Description
2. Assignment Operators:
the right expression's value is assigned to the left operand.
Python offers different assignment operators to assign values to
the variable.
Example:
a = 34 # Initialize the value of a
b=6 # Initialize the value of b
3. Logical Operators
Python offers different types of logical operators such as and, or,
and not. In the case of the logical AND, if the first one is 0, it does
not depend upon the second one. In the case of the logical OR, if
the first one is 1, it does not depend on the second one.
4. Bitwise Operators
he two operands' values are processed bit by bit by the bitwise
operators. There are various Bitwise operators used in Python,
such as bitwise OR (|), bitwise AND (&), bitwise XOR (^),
negation (~), Left shift (<<), and Right shift (>>).
output:
a&b:0
a | b : 15
a ^ b : 15
~a : -8
a << b : 1792
a >> b : 0
5. Membership Operators
We can verify the membership of a value inside a Python data
structure using the Python membership operators. The result is
said to be true if the value or variable is in the sequence (list,
tuple, or dictionary); otherwise, it returns false.
# initializing a list
myList = [12, 22, 28, 35, 42, 49, 54, 65, 92, 103, 245, 874]
output:
Given List: [12, 22, 28, 35, 42, 49, 54, 65, 92, 103, 245, 874]
x = 31 is NOT present in the given list.
6. Identity Operators
Operators that are used to check if two values are located on the
same part of the memory.
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define
scope in the code. Other programming languages often use curly-brackets for
this purpose.
Example
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
a = 33
b = 200
if b > a:
print("b is greater than a")
>>name=”john”
Print(name)
output:
Good Morning students we are from MCA
Expressions in Python
An expression is a combination of operators and operands that
produces some other value. an expression is evaluated as per the precedence
of its operators. their precedence decides which operation will be performed
first
There are different types of expressions in Python.
1. Arithmetic Expressions
x = 2
y = 4
add = x + y
sub = x - y
pro = x * y
div = x / y
print(add)
print(sub)
print(pro)
print(div)
Operator Precedence
It is simple to process if there is only operator in an expression but if there are multiple
operators it is quite complex to evulate.it may give different results based on order of
evaluating the expression.
Operator Precedence simply defines the priority of operators that which operator is to be
executed first.
nt
Example:
Example:
Number=12
If number>10:
Print(“the number is greater then 10”)
Example:
age=18
If age>=18:
Print(“you are eligible to vote”)
else:
Print(“you are not eligible to vote”)
The if-elif-else
The if-elif-else provides a multi-condition functionality to evaluates multiple conditions
sequentially executing the first block whose condition evaluates to true. If none of the
conditions are true, the else block runs.
Syntax:
if condition 1:
# code is executed if the condition is true
elif condition 2:
# code is executed if the condition is true
elif condition 3:
# code is executed if the condition is true
else:
# code is executed if the condition is false
Example:
Marks=90
If marks >=90
Print(“Grade: A”)
elif marks >=75:
Print (“Grade: B”)
elif marks >=50:
print (Grade: C”)
else:
print(“Grade: F”)
Looping Statements
Looping statements in Python allow you to execute a code block based on a condition
repeatedly. They are essential when performing repetitive tasks like iterating through a
list.
Example:
Cars=[Toyoa,Maruthi,Hyundai,Mahindra]
For car in cars:
Print(cars)
Syntax:
While condition:
#executes if the condition is true
Example:
Count=1
While count<=5:
Print(count)
Count +=1
Jump Statements
Jump statements in Python allow you to control the flow of loops and conditional blocks by
altering the standard execution sequence. program immediately stops the current iteration and
exits the loop.
Syntax:
For items in iteration :
If condition:
break
Example:
For n in range(1,10):
If n ==5:
break
Print(n)
Syntax:
For items in iteration :
If condition:
Continue
Example:
For n in range(1,20):
If n %2==0:
Continue
Print(n)
Example:
For n in range(1,10):
If n ==5:
Pass
else:
Print(n)
Python Functions
Python Functions is a block of statements that return the specific task. The idea is to put
repeatedly done tasks together and make a function so that instead of writing the same code
again and again for different inputs, we can do the function calls to reuse code contained in it
over and over again.
Benefits of Using Functions
Increase Code Readability
Increase Code Reusability
Python Function Declaration
Types of Functions in Python
Built-in library function: These are Standard functions in Python that are available
to use.
User-defined function: We can create our own functions based on our requirements.
Creating a Function in Python
We can define a function with the def keyword. We can add any type of functionalities and
properties to it as we require. we can create Python function definition by using def keyword.
def fun():
print("Hello Students”)
def fun():
print(" How are you")
# Driver code to call a function
fun()
Output:
How are you
Python Function Syntax with Parameters
def function_name(parameter: data_type) -> return_type:
# body of the function
return expression
Example 1:
def add(num1: int, num2: int) -> int:
"""Add two numbers"""
num3 = num1 + num2
return num3
# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print( ans)
Output
20
Example:
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")
Output
even
odd
Example:
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)
myFun(10)
Output
x: 10
y: 50
2. Keyword Arguments
To specify the argument name with values so that the caller does not need to remember the
order of parameters.
def student(firstname, lastname):
print(firstname, lastname)
student(firstname=”Sai Sri”, lastname=” Reddy'')
student(lastname='Reddy', firstname='Sai Sri')
Output
Sai Sri Reddy
Reddy Sai Sri
Positional Arguments
We used the Position argument during the function call so that the first argument is assigned
to name and the second argument is assigned to age. By changing the position, or if you
forget the order of the positions, the values can be used in the wrong places.
def fun1(name, age):
print( name)
print( age)
fun1("SRI", 27)
fun1(27, "SRI")
Output
SRI 27
27 SRI
Output
HI
Output
first == M
mid == C
last == A
Anonymous Functions in Python
An anonymous function means that a function is without a name. As we already know the def
keyword is used to define the normal functions and the lambda keyword is used to create
anonymous functions.
EXAMPLE:
def cube(x): return x*x*x
cube_v2 = lambda x : x*x*x
print(cube(7))
print(cube_v2(7))
Output
343
343