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

Python Unit 1

Python is a high-level, interpreted programming language known for its simplicity, readability, and versatility. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python has a clear syntax that makes it easy to learn for beginners while being powerful enough for professionals to develop complex applications. It offers a wide range of standard libraries and frameworks that simplify tasks in web development, data analysis, artificial int

Uploaded by

abhay94111
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)
8 views10 pages

Python Unit 1

Python is a high-level, interpreted programming language known for its simplicity, readability, and versatility. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python has a clear syntax that makes it easy to learn for beginners while being powerful enough for professionals to develop complex applications. It offers a wide range of standard libraries and frameworks that simplify tasks in web development, data analysis, artificial int

Uploaded by

abhay94111
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/ 10

BCC302 / BCC402/ BCC302H / BCC402H :

PYTHON PROGRAMMING
Course Outcome ( CO) Bloom’s Knowledge Level (KL)
At the end of course , the student will be able to understand
Interpret the fundamental Python syntax and semantics and be fluent in the use of Python control flow
CO 1 K1, K2
statements.
CO 2 Express proficiency in the handling of strings and functions K1, K2
Determine the methods to create and manipulate Python programs by utilizing the data structures like
CO 3 K3
lists, dictionaries, tuples and sets.
CO 4 Identify the commonly used operations involving file systems and regular expressions. K1, K2
Articulate the Object-Oriented Programming concepts such as encapsulation, inheritance and
CO 5 K2, K3
polymorphism as used in Python
DETAILED SYLLABUS
Unit Topic Lecture
I Introduction to Python: Python variables, Python basic Operators, Understanding python
blocks. Python Data Types, Declaring and using Numeric data types: int, float etc. 03

Python Program Flow Control Conditional blocks: if, else and else if, Simple for loops in
II python, For loop using ranges, string, list and dictionaries. Use of while loops in python,
05
Loop manipulation using pass, continue, break and else. Programming using Python
conditional and loop blocks.
Python Complex data types: Using string data type and string operations, Defining list
and list slicing, Use of Tuple data type. String, List and Dictionary, Manipulations Building
III blocks of python programs, string manipulation methods, List manipulation. Dictionary 04
manipulation, Programming using string, list and dictionary in-built functions. Python
Functions, Organizing python codes using functions.
Python File Operations: Reading files, Writing files in python, Understanding read
IV functions, read(), readline(), readlines(). Understanding write functions, write() and 04
writelines() Manipulating file pointer using seek Programming, using file operations.
Python packages: Simple programs using the built-in functions of packages matplotlib,
V numpy, pandas etc. GUI Programming: Tkinter introduction, Tkinter and 04
PythonProgramming, Tk Widgets, Tkinter examples. Python programming with IDE.

Text books:
1. Wesley J. Chun, “Core Python Applications Programming”, 3rd Edition , Pearson Education, 2016
2. Lambert, Fundamentals of Python: First Programs with MindTap, 2nd 1st edition , Cengage Learning publication
3. Charles Dierbach, “Introduction to Computer Science using Python”, Wiley, 2015
4. Jeeva Jose &P.SojanLal, “Introduction to Computing and Problem Solving with PYTHON”, Khanna Publishers, New Delhi,
2016
5. Downey, A. et al., “How to think like a Computer Scientist: Learning with Python”, John Wiley, 2015
6. Mark Lutz, “Learning Python”, 5th edition, Orelly Publication, 2013, ISBN 978- 1449355739
7. John Zelle, “Python Programming: An Introduction to Computer Science”, Second edition, Course Technology Cengage
Learning Publications, 2013, ISBN 978- 1590282410
8. Michel Dawson, “Python Programming for Absolute Beginers” , Third Edition, Course Technology Cengage Learning
Publications, 2013, ISBN 978-1435455009
9. David Beazley, Brian Jones., “Python Cookbook”, Third Edition, Orelly Publication, 2013, ISBN 978-1449340377
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
3
XOR 5 =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
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)

You might also like