0% found this document useful (0 votes)
3 views9 pages

Unit 1- Introduction to Python Programming

Uploaded by

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

Unit 1- Introduction to Python Programming

Uploaded by

criteriacom23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit 1: Introduction to Python Programming

1. Introduction to Python Programming


What is Python?
Python is a high-level, interpreted, general-purpose programming language
developed by Guido van Rossum and released in 1991.
Python is widely used in:
 Web Development
 Data Science
 Artificial Intelligence
 Machine Learning
 Cyber Security
 Automation
 Scientific Computing
Features of Python
1. Simple and Easy to Learn
2. Open Source
3. Interpreted Language
4. Platform Independent
5. Object-Oriented
6. Large Standard Library
7. Supports GUI Programming
8. Dynamically Typed
Advantages of Python
 Easy syntax
 Faster development
 Readable code
 Large community support
 Extensive libraries and frameworks

2. Variables and Expressions


Variables
A variable is a named memory location used to store data.
Syntax
variable_name = value

Example
name = "John"
age = 20
salary = 25000.50

Rules for Naming Variables


 Must start with a letter or underscore (_)
 Cannot start with a number
 Cannot contain special characters except underscore
 Python keywords cannot be used
Valid Variables
name = "Rahul"
_age = 21
total_marks = 90

Invalid Variables
1name = "ABC"
class = 10

3. Values and Types


A value is the data stored in a variable.

Common Data Types


Data Type Example
int 10
float 10.5
str “Python”
bool True
complex 2+3j

Example
x = 10
y = 12.5
name = "Python"
status = True
Checking Data Type
x = 10
print(type(x))

Output:
<class 'int'>

4. Type Conversion
Type conversion means converting one data type into another.

Implicit Type Conversion


Performed automatically by Python.
x = 10
y = 5.5
z = x + y

print(z)
print(type(z))

Output:
15.5
<class 'float'>

Explicit Type Conversion


Performed using conversion functions.

Function Description
int() Converts to integer
float() Converts to float
str() Converts to string
bool() Converts to Boolean

Example
x = "100"

y = int(x)

print(y)
print(type(y))
Output:
100
<class 'int'>

5. Operators and Operands


An operator performs an operation on operands.

Example
a = 10
b = 5

c = a + b

Here:

o is Operator
 a and b are Operands

Types of Operators
1. Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
// Floor Division
** Exponentiation

Example:
a = 10
b = 3

print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b)
print(a//b)
print(a**b)

2. Relational Operators
Operator Meaning
== Equal
!= Not Equal
> Greater Than
< Less Than
>= Greater Than Equal To
<= Less Than Equal To

Example:
a = 10
b = 20

print(a > b)

3. Logical Operators
Operator Meaning
and Logical AND
or Logical OR
not Logical NOT

Example:
a = True
b = False

print(a and b)
print(a or b)
print(not a)

4. Assignment Operators
Operator Example
= x = 10
+= x += 5
-= x -= 5
Operator Example

*= x *= 5
/= x /= 5

6. Conditional Statements
Conditional statements are used to make decisions.

if Statement
Syntax
if condition:
statement

Example
age = 18

if age >= 18:


print("Eligible for Voting")

if-else Statement
Syntax
if condition:
statement1
else:
statement2

Example
num = 10

if num % 2 == 0:
print("Even")
else:
print("Odd")

Nested if-else
An if statement inside another if statement is called nested if.
Example
num = 10

if num > 0:
if num % 2 == 0:
print("Positive Even Number")
else:
print("Positive Odd Number")
else:
print("Negative Number")

7. Looping
Loops are used to execute a block of code repeatedly.

for Loop
Used when the number of iterations is known.

Syntax
for variable in sequence:
statements

Example
for i in range(1,6):
print(i)

Output:
1
2
3
4
5

while Loop
Used when the number of iterations is not known.

Syntax
while condition:
statements
Example
i = 1

while i <= 5:
print(i)
i += 1

Nested Loops
A loop inside another loop is called a nested loop.

Example
for i in range(1,4):
for j in range(1,4):
print(i,j)

Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

8. Control Statements
Control statements alter the normal flow of loops.

break Statement
Terminates the loop immediately.

Example
for i in range(1,10):
if i == 5:
break
print(i)

Output:
1
2
3
4

continue Statement
Skips the current iteration and moves to the next iteration.

Example
for i in range(1,6):
if i == 3:
continue
print(i)

Output:
1
2
4
5

Difference Between break and continue


break continue
Terminates loop Skips current iteration
Stops execution Continues execution
Exits loop Remains inside loop

Summary
 Python is a high-level, interpreted language.
 Variables store values in memory.
 Data types define the nature of data.
 Type conversion changes one data type to another.
 Operators perform computations.
 Conditional statements enable decision making.
 Loops allow repetitive execution.
 break terminates a loop.
 continue skips the current iteration.

You might also like