0% found this document useful (0 votes)
6 views10 pages

Python Unit 1

Uploaded by

Mohammed Sahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views10 pages

Python Unit 1

Uploaded by

Mohammed Sahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Introduction to Python Programming

Syllabus of Chapter 1: Introduction to Python


Programming
1. Introduction to Python
• Overview of Python
• History and Features
2. Python Variables
• Rules for variable names
• Assigning values to variables
• Global and local variables
3. Python Basic Operators
• Arithmetic operators
• Comparison operators
• Assignment operators
• Bitwise operators
• Logical operators
4. Understanding Python Blocks
• Indentation
• Code blocks
5. Python Data Types
• Numeric types
• Sequence types
• Mapping type
• Set types
• Boolean type
• Binary types
• None type

1
Notes for Chapter 1: Introduction to Python Pro-
gramming
Introduction to Python
Python is an interpreted, object-oriented, high-level programming language cre-
ated by Guido van Rossum. Python’s design philosophy emphasizes code read-
ability and simplicity. It has a simple and easy-to-use syntax, making it a
popular choice for beginners. It supports Object-Oriented programming, which
encapsulates code within objects. Python is dynamically typed and garbage-
collected. It has a large standard library that supports many common program-
ming tasks.

Python Variables
Variables are containers that store values. For example:
name = "John"
age = 25

Rules for Python variable names:


• Must start with a letter or the underscore character.
• Cannot start with a number.

• Can only contain alpha-numeric characters and underscores (A-z, 0-9, and
).
• Are case-sensitive.
• Reserved words (keywords) cannot be used as variable names.

Global and Local Variables


Global variables are defined outside of any function and can be accessed any-
where in the code. Local variables are defined inside a function and can only
be accessed within that function. For example:
x = "global"

def my_function():
x = "local"
print(x)

my_function()
print(x)

2
Python Basic Operators
Arithmetic Operators
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus: %
Exponentiation: **
Floor division: //

Operator Description Example


+ Addition 5+3=8
- Subtraction 5-3=2
* Multiplication 5 * 3 = 15
/ Division 6/3=2
** Exponentiation 2 ** 3 = 8
// Floor Division 7 // 2 = 3

Table 1: Arithmetic Operators

Comparison Operators
Equal: ==
Not equal: !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=

Operator Description Example


== Equal to 5 == 5
!= Not equal to 5 != 3
¿ Greater than 5¿3
¡ Less than 3¡5
¿= Greater than or equal to 5 ¿= 5
¡= Less than or equal to 3 ¡= 5

Table 2: Comparison Operators

Assignment Operators
Assign: =

3
Add and assign: +=
Subtract and assign: -=
Multiply and assign: *=
Divide and assign: /=
Modulus and assign: %=
Exponentiation and assign: **=
Floor division and assign: //=

Operator Description Example


= Assign x=5
+= Add and assign x += 3 (x = x + 3)
-= Subtract and assign x -= 3 (x = x - 3)
*= Multiply and assign x *= 3 (x = x * 3)
/= Divide and assign x /= 3 (x = x / 3)
**= Exponentiation and assign x **= 3 (x = x ** 3)
//= Floor division and assign x //= 3 (x = x // 3)

Table 3: Assignment Operators

Bitwise Operators
AND: &
OR: |
XOR: ^
NOT: ~
Zero fill left shift: <<
Signed right shift: >>

Operator Description Example


AND
5 3=1
— OR 5—3=7
XOR 53=6
NOT 5 = -6
¡¡ Zero fill left shift 5 ¡¡ 1 = 10
¿¿ Signed right shift 5 ¿¿ 1 = 2

Table 4: Bitwise Operators

Logical Operators
AND: and
OR: or
NOT: not

4
Operator Description Example
and Logical AND True and False = False
or Logical OR True or False = True
not Logical NOT not True = False

Table 5: Logical Operators

Understanding Python Blocks


Python uses indentation to define blocks of code. The standard indentation is
4 spaces (or a tab). For example:
for i in range(5):
print(i)
print("Outside the loop")

Python Data Types


Numeric Types:
• int: Integer numbers
• float: Floating point numbers
• complex: Complex numbers

Sequence Types:
• str: String type
• list: List type

• tuple: Tuple type


• range: Range type
Mapping Type:
• dict: Dictionary type

Set Types:
• set: Set type
• frozenset: Immutable set type

Boolean Type:
• bool: Boolean type, can be True or False
Binary Types:

5
• bytes: Byte type
• bytearray: Byte array type
• memoryview: Memory view type
None Type:

• NoneType: Represents the absence of a value or a null value

Figure 1: An example of Python data types

Important Questions
1. What are the key features of Python?

2. Explain the difference between local and global variables with an example.
3. List and explain different arithmetic operators in Python with examples.
4. What is the significance of indentation in Python? Provide an example.

5. Explain different data types available in Python with examples.

Answers
1. Key features of Python:

• Interpreted
• Object-oriented
• High-level language

6
• Dynamically typed
• Garbage-collected
• Large standard library
• Simple and readable syntax

2. Local and Global Variables:

x = "global"

def my_function():
x = "local"
print(x)

my_function() # Output: local


print(x) # Output: global

3. Arithmetic Operators:

# Addition
x = 5 + 3
print(x) # Output: 8

# Subtraction
x = 5 - 3
print(x) # Output: 2

# Multiplication
x = 5 * 3
print(x) # Output: 15

# Division
x = 6 / 3
print(x) # Output: 2.0

# Modulus
x = 5 % 3
print(x) # Output: 2

# Exponentiation
x = 2 ** 3
print(x) # Output: 8

# Floor Division
x = 7 // 2

7
print(x) # Output: 3

4. Significance of Indentation:

for i in range(5):
print(i)
print("Outside the loop")

Indentation is used to define blocks of code. The standard indentation is


4 spaces (or a tab).
5. Data Types:

# Numeric Types
x = 10 # int
y = 10.5 # float
z = 1 + 2j # complex

# Sequence Types
a = "Hello" # str
b = [1, 2, 3] # list
c = (1, 2, 3) # tuple
d = range(5) # range

# Mapping Type
e = {"a": 1, "b": 2} # dict

# Set Types
f = {1, 2, 3} # set
g = frozenset({1, 2, 3}) # frozenset

# Boolean Type
h = True # bool

# Binary Types
i = b"hello" # bytes
j = bytearray(5) # bytearray
k = memoryview(bytes(5)) # memoryview

# None Type
l = None # NoneType

8
Important Code Examples
Example 1: Basic Python Program
print("Hello, World!")

Example 2: Function Definition and Call


def greet(name):
return "Hello, " + name

print(greet("Alice"))

Example 3: Conditional Statements


x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")

Example 4: Looping
for i in range(5):
print(i)

Example 5: Working with Lists


my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)

Thank You

9
Figure 2: *
Thank You!

10

You might also like