Cad 7121 - Python Programming - Module - 1
Cad 7121 - Python Programming - Module - 1
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
FEATURES OF PYTHON
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
WHY TO LEARN PYTHON?
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
FEATURES OF PYTHON
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
DATA TYPES: NUMBERS, STRINGS & ITS OPERATIONS
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
DATA TYPES: NUMBERS, STRINGS & ITS OPERATIONS
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Values and Variables
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
INTEGER VALUES
• The number four (4) is an example of a numeric value.
• In mathematics, 4 is an integer value.
• Integers are whole numbers, which means they have no
fractional parts, and they can be positive, negative, or zero.
• Examples of integers include 4, −19, 0, and −1005.
• In contrast, 4.5 is not an integer, since it is not a whole
number.
• Python supports a number of numeric and non-numeric
values.
• In particular, Python programs can use integer values.
• The Python statement print(4) prints the value 4
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Example –Integer value entered in python editor and its behavior
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Use of + symbol for normal arithmetic addition
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Use of Int() & str() functions in python
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
How + operator works with string
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
“type” function to find the expression type(whether its int or str)
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Variables and Assignment
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Python Identifiers
• A Python identifier is a name used to identify
– a variable,
– function,
– class, module
– or other object.
• An identifier starts with a letter
– A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
• Python does not allow punctuation characters such as @, $, and %
within identifiers.
• Python is a case sensitive programming language.
Thus, Manpower and manpower are two different identifiers in Python.
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Floating-point Types
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
STRING
String data type written is python in single or double code
x = ‘hello’
y = “Crescent”
z = input()
print(“ here is an example”, x, y, z)
Name = “Crescent”
Len(Name)
Output : ?
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
STRING
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Python Strings
String Literals
String literals in python are surrounded by either single quotation marks, or double
quotation marks.
Example
print("Hello")
print('Hello')
You can assign a multiline string to a variable by using three double quotes Or three single
quotes:
Example
You can use three double quotes:
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Strings are Arrays
Like many other popular programming languages, strings in Python are arrays of bytes
representing unicode characters.
However, Python does not have a character data type, a single character is simply a string
with a length of 1.
Example
Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a[1])
String Methods
Python has a set of built-in methods that you can use on strings.
Example
The strip() method removes any whitespace from the beginning or the end
:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
a = "Hello, World!"
print(a.upper())
String Methods
a = "Hello, World!"
print(a.replace("H", "J"))
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
Check String
Example
String Concatenation
Example Example
Merge variable a with variable b into variable c: To add a space between them, add a " ":
a = "Hello" a = "Hello"
b = "World" b = "World"
c=a+b c=a+""+b
print(c) print(c)
Write a Python program to check whether two strings are equal or not.
PROGRAM:
OUTPUT:
PROGRAM:
# Python Program to Reverse String
OUTPUT:
%d – Integers
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right
of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
Control codes within strings.
The characters that can appear within strings include letters of the alphabet
(A-Z, a-z), digits (0-9), punctuation (., :, ,, etc.), and other printable symbols
(#, &, %, etc.).
In addition to these “normal” characters, we may embed special characters
known as control codes.
Control codes control the way text is rendered in a console window or paper
printer.
The backslash symbol (\) signifies that the character that follows it is a
control code, not a literal character.
The string ’\n’ thus contains a single control code. The backslash is known as
the escape symbol, and in this case we say the n symbol is escaped.
The \n control code represents the newline control code which moves the
text cursor down to the next line in the console window. Other control codes
include \t for tab, \f for a form feed (or page eject) on a printer, \b for
backspace, and \a for alert (or bell)
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Control codes within strings Example Program
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
BOOLEAN EXPRESSION & OPERATORS
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Boolean Expressions
• Arithmetic expressions evaluate to numeric
values; a Boolean expression, sometimes called
a predicate, may have only one of two possible
values: false or true.
• The simplest Boolean expressions in Python are
True and False.
Boolean Expressions
Example : Boolean Expressions
EXPRESSIONS AND ARITHMETIC
3.1 Expressions
A literal value like 34 and a variable like x are examples of a simple expressions. Values
and variables can be combined with operators to form more complex expressions.
** Exponent - left operand raised to the power of right x**y (x to the power y)
Example #1: Arithmetic operators in Python
ALGORITHM:
STEP 1: Display choice of operation in screen, “Choice 1: Add, 2: Sub, 3: Div, 4: Mul, 5: Quit “.
STEP 4: If choice of option is 1 then receive input for A and B, Initiate operation c = A + B.
STEP 5: If choice of option is 2 then receive input for A and B, Initiate operation c = A - B.
STEP 6: If choice of option is 3 then receive input for A and B, Initiate operation c = A / B.
STEP 7: If choice of option is 4 then receive input for A and B, Initiate operation c = A * B.
PROGRAM:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
FIZZ BUZZ PROGRAM
3. Write a program to incorporate FIZZ for any number divisible by 3 and Buzz for any
number divisible for 5 and FIZZBUZZ for any number divisible by 3 and 5 as well.
ALGORITHM:
STEP 1: Print “FizzBuzz Program”
STEP 2: prompt for user input for value N1
STEP 3: Convert the received value N1 to integer value N
STEP 4: Create a loop which will execute from 0 to N+1 times.
If I modules 3 and I modules 5 is zero then
Print I value + “= FIZZBUZZ”
If I modules 3 is zero then
Print I value + “= FIZZ”
If I modules 5 is zero then
Print I value + “= Buzz”
else
Print I Value
PROGRAM: OUTPUT:
print ("Fizz Buzz Program :") FizzBuzz Program
n1 = input("Enter the number : ") Enter the number: 15
0
n = int(n1) 1
i=0 2
3 = FIZZ
for i in range (n+1): 4
if (i % 3 == 0 and i % 5 == 0): 5 = BUZZ
6
print (str(i) + "= Fizz Buzz") 7
elif (i % 3 == 0): 8
9 = FIZZ
print (str(i) + "= Fizz ") 10 = BUZZ
elif (i % 5 == 0): 11
12 = FIZZ
print (str(i) + "= Buzz ") 13
else: 14
15 = FIZZBUZZ
print(i)
LIST & ITS OPERATIONS, TUPLES & ITS OPERATIONS,
DICTIONARIES & ITS OPERATIONS
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Python Lists
Tuple
Array: An array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together. This makes it easier to calculate the position of each element by simply
adding an offset to a base value, i.e., the memory location of the first element of the array (generally
denoted by the name of the array). The following are the main characteristics of an Array:
An array is an ordered collection of the similar data types.
An array is mutable.
An array can be accessed by using its index number.
List: A list is of an ordered collection data type that is mutable which means it can be easily modified and we
can change its data values and a list can be indexed, sliced, and changed and each element can be accessed
using its index value in the list. The following are the main characteristics of a List:
The list is an ordered collection of data types.
The list is mutable.
List are dynamic and can contain objects of different data types.
List elements can be accessed by index number.
Tuple: A tuple is an ordered and an immutable data type which means we cannot change its values and
tuples are written in round brackets. We can access tuple by referring to the index number inside the square
brackets. The following are the main characteristics of a Tuple:
Tuples are immutable and can store any type of data type.
it is defined using ().
it cannot be changed or replaced as it is an immutable data type.
Python Tuples
Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to
the second last item etc.
Python Tuples
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the
range.
When specifying a range, the return value will be a new tuple with the specified
items.
Note: The search will start at index 2 (included) and end at index 5 (not included).
Python Tuples
Specify negative indexes if you want to start the search from the end of the tuple:
Tuple Length
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
INPUT-OUTPUT
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
User Input
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
User Input- Example
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
eval()
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
The eval function: eval()
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Output : Implementation of eval() Output : Implementation of eval()
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
CONDITIONS STATEMENTS: IF, IF-ELSE, IF-ELIF-ELSE –
LOOPING STATEMENTS: WHILE, FOR
CAC
CAD 2216 – UNITProgramming
7121 -Python II Course Instructor : Dr.A.Abdul Azeez Khan & Dr. K.Javubar Sathick
Simple if statement & if/else.
• if statement allows code to be optionally
executed
Simple if statement(flowchart & Syntax)
Simple if statement
• Python requires the block to be indented. If the block contains just one
statement, some programmers will place it on the same line as the if;
for example, the following if statement that optionally assigns y
if x < 10:
y=x
could be written as
if x < 10: y = x
• because the lack of indentation hides the fact that the assignment
statement is optionally executed.
• Indentation is how Python determines which statements make up a
block.
Using spaces and tabs in python editor
• It is important not to mix spaces and tabs when indenting
statements in a block.
• In many editors you cannot visually distinguish between a tab
and a sequence of spaces.
• The number of spaces equivalent to the spacing of a tab differs
from one editor to another.
• Most programming editors have a setting to substitute a
specified number of spaces for each tab character.
• For Python development you should use this feature.
• It is best to eliminate all tabs within your Python source code
• How many spaces should you indent?
• Python requires at least one, some programmers consistently
use two, four is the most popular number, but some prefer a
more dramatic display and use eight.
• A four space indentation for a block is the recommended
Python style.
If with multiple statement
The if/else Statement
The if statement has an optional else clause that is
executed only if the Boolean condition is false.
if else statement(flowchart & Syntax)
if & else (compare & Contrast)
Nested Conditionals
• The statements in the block of the if or the else may be any
Python statements, including other if/else statements.
•These nested if statements can be used to develop arbitrarily
complex control flow logic.
Nested Conditionals(using and logic)