0% found this document useful (0 votes)
15 views

unit-1

Unit I introduces Python as a high-level, interpreted programming language known for its readability and simplicity, covering its history, features, and applications. It explains fundamental building blocks such as indentation, identifiers, variables, comments, and operators, along with basic input/output operations. Additionally, it discusses control flow statements, including conditional statements and loops, which are essential for controlling the execution flow of Python programs.

Uploaded by

01volley04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

unit-1

Unit I introduces Python as a high-level, interpreted programming language known for its readability and simplicity, covering its history, features, and applications. It explains fundamental building blocks such as indentation, identifiers, variables, comments, and operators, along with basic input/output operations. Additionally, it discusses control flow statements, including conditional statements and loops, which are essential for controlling the execution flow of Python programs.

Uploaded by

01volley04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Detailed Theory Explanation: Unit - I Introduction to

Python and Control Flow Statements

1.1 Introduction to Python Python is a high-level,


interpreted programming language that emphasizes
code readability and simplicity. It was created by Guido
van Rossum and released in 1991. Python supports
multiple programming paradigms, including procedural,
object-oriented, and functional programming. Python's
syntax is clean and its community is vast, making it
ideal for beginners as well as professionals.
Features of Python:
• Easy to read and write
• Dynamically typed
• Extensive standard libraries
• Platform-independent
• Supports GUI programming
History of Python:
• Late 1980s: Development started by Guido van
Rossum.
• 1991: Python 0.9.0 released.
• 2000: Python 2.0 released with new features like
garbage collection.
• 2008: Python 3.0 released (not backward
compatible with Python 2).
Applications of Python:
• Web development (Django, Flask)
• Data Science and Machine Learning (NumPy,
Pandas, Scikit-learn)
• Automation and scripting
• Game development (Pygame)
• Embedded systems and IoT
Python IDEs:
• IDLE: Default IDE that comes with Python
• PyCharm: Full-featured IDE
• Visual Studio Code: Lightweight and versatile
• Jupyter Notebook: Great for data science and
research
• Thonny: Ideal for beginners
1.2 Python Building Blocks
python Building Blocks refer to the basic elements and
rules you need to know before you can start writing
Python programs. Just like how a house is built using
bricks and materials, a Python program is built using
these fundamental components.
Indentation: In Python, indentation (spaces or tabs) is
used to define blocks of code. Unlike other languages
that use braces ({}), Python relies on consistent
indentation.
Example:
if True:
print("This is indented correctly")

Identifiers: Names used to identify variables, functions,


classes, etc. Must start with a letter (A-Z or a-z) or an
underscore (_), followed by letters, digits, or
underscores.
Rules:
• Must begin with a letter (A-Z or a-z) or an
underscore (_)
• Can be followed by letters, digits (0-9), or
underscores
• Case-sensitive (e.g., name and Name are different)
• Cannot use Python keywords (like if, else, for)
student_name = "Aarti"
age = 20
Variables: Used to store data. Python is dynamically
typed, so you don't need to declare a variable type
explicitly.
x = 10
name = "Alice"
Comments: Used to explain code. Not executed by the
interpreter.
• Single-line: # This is a comment
• Multi-line: """ This is a multi-line comment """
Example:
# This is a single-line comment

"""
This is a
multi-line comment
"""
Keywords: Reserved words in Python that cannot be
used as identifiers. Example: if, else, while, class, def,
etc.
1.3 Basic Input and Output Operations
1. Input Operation – input()
• The input() function is used to take input from the
user.
• It always returns a string by default.
Syntax:
variable = input("Enter something: ")
Example:
name = input("Enter your name: ")
print("Hello", name)
If you need numeric input, you must convert it
using int() or float():
age = int(input("Enter your age: "))
2. Output Operation – print()
• The print() function is used to display output on
the screen.
Syntax:
print("Your message here")
Examples:
print("Welcome to Python!")
name = "Aarti"
print("Hello", name)
3. Formatted Output using f-strings
• Python allows you to format strings using f-strings
(available from Python 3.6+).
Example:
name = "Aarti"
age = 20
print(f"My name is {name} and I am {age} years old.")
1.4 Operators in Python
What are Operators?
Operators are special symbols in Python that perform
operations on values and variables. Python has many
types of operators, each used for a specific purpose.

1. Arithmetic Operators
Used to perform basic math operations.
Operator Name Example Output
+ Addition 10 + 5 15
- Subtraction 10 - 3 7
* Multiplication 4*3 12
/ Division 10 / 2 5.0
// Floor Division 10 // 3 3
% Modulus (remainder) 10 % 3 1
** Exponentiation 2 ** 3 8

2. Relational (Comparison) Operators


Used to compare two values and return a boolean
result (True or False).
Operator Description Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7>5 True
< Less than 4<9 True
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 3 <= 6 True

3. Assignment Operators
Used to assign values to variables and modify them.
Operator Example Meaning
= x = 10 Assign 10 to x
+= x += 5 x=x+5
-= x -= 2 x=x-2
*= x *= 3 x=x*3
/= x /= 2 x=x/2
//=, %=, **= Similar operations
4. Logical Operators
Used to combine multiple conditions.
Operator Description Example Output
True if both conditions x > 2 and x
and True
are true < 10
True if at least one x < 2 or x >
or False
condition is true 10
not Reverses the result not(x > 5) Depends

5. Bitwise Operators
Used for binary-level operations.
Operator Name Example Output
& AND 5&3 1
` ` OR `5
^ XOR 5^3 6
~ NOT ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2
6. Membership Operators
Used to test if a value is part of a sequence (like string,
list).
Operator Example Output
in 'a' in 'apple' True
not in 'x' not in 'apple' True

7. Identity Operators
Used to compare the memory location of two objects.
Operator Example Output
is a is b True/False
is not a is not b True/False

Example Program Using All Types:


x = 10
y=5

print(x + y) # Arithmetic
print(x > y) # Relational
x += 2
print(x) # Assignment
print(x > 5 and y < 10) # Logical
print('a' in 'apple') # Membership
print(x is y) # Identity

1.5 Control Flow Statements


What are Control Flow Statements?
Control flow statements allow you to change the
execution flow of a Python program.
They help make decisions, repeat tasks, and control
how loops behave.
Python has three main types of control flow:

1. Conditional Statements (Decision Making)


➤ if Statement
Executes a block of code only if the condition is true.
age = 18
if age >= 18:
print("You are eligible to vote")
➤ if-else Statement
Provides an alternative block if the condition is false.
marks = 40
if marks >= 50:
print("Pass")
else:
print("Fail")

➤ if-elif-else Statement
Checks multiple conditions.
num = 0
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")

➤ Nested if
An if inside another if.
x = 10
if x > 0:
if x < 20:
print("x is between 1 and 19")

2. Loops in Python
Used to repeat a block of code multiple times.
➤ while Loop
Repeats while a condition is true.
i=1
while i <= 5:
print(i)
i += 1

➤ for Loop
Used to iterate over sequences like list, tuple, string.
for i in range(1, 6):
print(i)

➤ Nested Loops
A loop inside another loop.
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")

3. Loop Manipulation Statements


Used to alter the normal behavior of loops.
➤ break
Exits the loop immediately.
for i in range(5):
if i == 3:
break
print(i)
Output: 0 1 2

➤ continue
Skips the current iteration.
for i in range(5):
if i == 2:
continue
print(i)
Output: 0 1 3 4

➤ pass
Does nothing – used as a placeholder.
for i in range(5):
if i == 3:
pass
print(i)

➤ else with Loops


Runs if the loop does not break.
for i in range(3):
print(i)
else:
print("Loop completed without break")

for i in range(5):
print(i)
else:
print("Loop finished successfully")

This completes the detailed theory for Unit I.

You might also like