Python 1
Python 1
Introduc on to Python
Python is a high-level, interpreted programming language known for its simplicity and readability.
Created by Guido van Rossum in 1991, Python emphasizes code readability with its clean syntax and
indenta on. It is widely used for web development, data analysis, ar ficial intelligence, scien fic
compu ng, automa on, and more.
Advantages of Python
2. Used in various domains like web development, data science, AI, etc.
Disadvantages of Python
Basic Syntax:
Python uses indenta on to define blocks of code instead of curly braces {}.
print("Hello, World!")
Rule:
Example:
# Valid variables
name = "Anshu"
_age = 25
number1 = 100
# Invalid variables
1name = "Bob"
my-name = "Charlie"
An iden fica on error occurs in Python when the interpreter cannot recognize or correctly iden fy a
variable, keyword, or structure due to improper syntax, misuse, or naming conflicts.
Name = "Anshu"
print (name)
Python Variables
What is a Variable?
A variable in Python is a container used to store data. It acts as a name or reference for a memory
loca on where the data is stored. Variables make it easy to reuse and manipulate data in a program.
Syntax
variable_name = value
Examples
Type of Variable:
Local Variable: Declared inside a func on and accessible only within that func on.
Example:
x = "global"
x = "local"
my_func on()
Python provides built-in data types to classify the type of data a variable holds. These data types
determine the opera ons that can be performed on the variable.
1. Numeric Types
Example:
x = 10 # int
y = 10.5 # float
z = 3 + 4j # complex
2. Text Type
3. Sequence Types
Example:
4. Boolean Type
Example:
is_valid = True
A block in Python is a group of related statements intended to perform a specific task. Python uses
indenta on to define blocks of code, unlike many other programming languages that use curly
braces {}.
3. Logical Grouping
else:
2. Loop Blocks
Defined by for or while loops.
for i in range(5):
print(i)
3. Func on Blocks
Defined using the def keyword.
def greet(name):
print("Hello,", name)
greet("Alice")
1. PyCharm:
o Offers advanced features like debugging, tes ng, and integra on with frameworks
like Django.
3. Jupyter Notebook:
3. Combines wri ng, tes ng, and debugging in one pla orm.
3. Save the file and run it directly using the "Run" bu on in the IDE.
Operators in Python are special symbols or keywords used to perform opera ons on variables and
values. Python supports several types of operators that make it easy to perform mathema cal,
logical, or bitwise opera ons.
1. Arithme c Operators
+ Addi on 5+3=8
- Subtrac on 5-3=2
* Mul plica on 5 * 3 = 15
/ Division 5 / 3 = 1.6667
// Floor Division 5 // 3 = 1
% Modulus (Remainder) 5 % 3 = 2
** Exponen a on 5 ** 3 = 125
Example:
a = 10
b=3
print(a + b)
print(a % b)
2. Comparison Operators
== Equal to 5 == 3 (False)
Example:
x=7
y = 10
3. Logical Operators
and Returns True if both condi ons are true (5 > 3) and (7 > 4) (True)
Example:
a = True
b = False
4. Assignment Operators
= x=5 -
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
//= x //= 3 x = x // 3
%= x %= 3 x=x%3
**= x **= 3 x = x ** 3
Example:
x = 10
x += 5 # Equivalent to x = x + 5
print(x) # Output: 15
5. Bitwise Operators
` ` OR
^ XOR 5^3=6
~ NOT ~5 = -6
Example:
a = 5 # Binary: 101
b = 3 # Binary: 011
6. Membership Operators
not in Returns True if a value does not exist in a sequence 'x' not in 'apple' (True)
Example:
The floor division operator // in Python is used to perform division and return the largest integer
less than or equal to the result (i.e., the floor of the division result).
Example:
print(10 // 3) # Output: 3
Data types in Python represent the kind of data stored in a variable. Python provides various built-in
data types to handle different kinds of data efficiently.
complex: Represents complex numbers (numbers with real and imaginary parts).
1. Integer (int)
Example:
x = 10
y = -50
print(type(x))
print(type(y))
sum_result = x + y
print(sum_result)
2. Float (float)
Example:
pi = 3.14159
temperature = -20.5
print(type(pi))
print(type(temperature))
print(average)
3. Complex (complex)
Example:
z = 2 + 3j
w = 1 - 2j
print(type(z))
sum_result = z + w
print(sum_result)
Explain the concept of dynamic python program with suitable example. (2023-24)
A dynamic Python program refers to a program that can change its behaviour during run me. Unlike
sta c programming, where the structure and behaviour are defined beforehand, dynamic
programming involves flexibility in how the program func ons, adapts, and interacts with its
environment during execu on.
Key Features:
2. Dynamic Memory Alloca on: Python handles memory management automa cally.
3. Dynamic Func onality: Func ons and variables can change during execu on.
4. Dynamic Imports and Data Structures: Modules and data structures can be modified at
run me.
Example:
if isinstance(value, int):
return value * 2
return value[::-1]
print(dynamic_func on(10))
print(dynamic_func on("hello"))
/ Operator // Operator
It is floa ng-point division. It is integer division (floor of the quo ent).
It performs division and returns the exact It performs division and rounds down to the
result. nearest integer.
It return both float and integer type. It return only integer type.
Ex: 7 / 3 = 2.3333333333333335 Ex: 7 // 3 = 2
3. Tes ng: Test the code thoroughly (unit, integra on, system).