Unit 1
Unit 1
Unit: I
Conditional Statements:
if statement, if-else statement,
Learn the conditional statement like simple if, if-else, nested if-else
and elif ladder.
Learn the basic python data structures like string, list, tuple, set and
dictionary.
Study the file and exception handling to develop real life application.
Sachin Kumar Problem Solving using Python Unit I
2/15/2022 10
Course Outcome (CO)
Course Blooms
Outcome At the end of course , the student will be able to: Knowledge
( CO) Level (KL)
CO1 Analyse and implement simple python programs. K3, K4
PO8 : Ethics
PO10 : Communication
INPUT OUTPUT
Data PROCESS Information
Instructions
Storage Speed
Computers
No IQ/ No
Accuracy
Feeling
Versatility
Information
Data Input Unit Main Memory Output Unit
or Result
Control Unit
Memory
Sachin Kumar Problem Solving using Python Unit I
2/15/2022 CPU Unit 30
Algorithm and Flowchart (CO1)
Ellipse/Oval Start/End
Diamond Condition
The Code is designed to inspire and guide the ethical conduct of all
computing professionals, including current and aspiring
practitioners, instructors, students, influencers, and anyone who
uses computing technology in an impactful way.
2. PROFESSIONAL RESPONSIBILITIES
A computing professional should…
• 2.1 Strive to achieve high quality in both the processes and
products of professional work.
• 2.2 Maintain high standards of professional competence, conduct,
and ethical practice.
• 2.3 Know and respect existing rules pertaining to professional
work.
• 2.4 Accept and provide appropriate professional review.
2. PROFESSIONAL RESPONSIBILITIES
• 2.5 Foster public awareness and understanding of computing,
related technologies, and their consequences.
• 2.6 Access computing and communication resources only when
authorized or when compelled by the public good.
• 2.7 Design and implement systems that are robustly and usably
secure.
• After that what he did as very clever. He had taken the syntax of
ABC, and some of its good features.
• The inspiration for the name came from BBCs TV Show – Monty
Pythons Flying Circus, as he was a big fan of the TV show and also he
wanted a short, unique and slightly mysterious name for his invention
and hence he named it Python!
• Object-oriented
– Everything in Python is an object.
– Object oriented programming (OOP) helps to solve a complex problem
intuitively.
– Structure supports such concepts as polymorphism, operation overloading, and
multiple inheritance.
– https://www.spyder-ide.org/
• IDLE
– https://docs.python.org/3/library/idle.html
• Sublime Text 3
– https://www.sublimetext.com/3
• Jupyter
– https://jupyter.org/install.html
2. Write jupyter notebook in command prompt and then press enter key.
5. Click on the new option on the right most corner and select python3 from
cursor. Untitled notebook will open up, you can change up the name by
selecting it.
2/15/2022 Sachin Kumar Problem Solving using Python Unit I 74
Elements of Python (CO1)
6. Click on the code cell write down the code 1+1 and then press
shift + Enter from keyboard/run command from interface. Then
check the output.
The students will study the elements of Python like Keywords and
Identifiers, Variables, Data types and Operators.
• Expression
• Operators
2/15/2022 85
Data types (CO1)
2/15/2022 86
Standard Data types (CO1)
2/15/2022 87
Numbers (CO1)
• They store numeric values. Number objects are created when a value is
assigned to them. For ex:
>>> var1 = 1
>>> var2 = 10
• They can be deleted the reference to a number object by using the del
statement. The syntax of the del statement is
del var1[,var2[,var3[....,varN]]]]
• A single object or multiple objects can be deleted by using the del statement.
For example
>>> del var
>>> del var_a, var_b
• For example
>>> str = Hello World!
>>> print(str) # Prints complete string
>>> print(str[0]) # Prints first character of the string
>>> print(str[2:5]) # Prints characters starting from 3rd to 5th
>>> print(str[2:]) # Prints string starting from 3rd character
>>> print(str * 2) # Prints string two times
>>> print(str + TEST) # Prints concatenated string
Slicing
• A range of characters can be returned by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to
return a part of the string.
Example
Get the characters from position 2 to position 5 (not included):
Program
b = "Hello, World!"
print(b[2:5])
Program Output
llo
2/15/2022 Sachin Kumar Problem Solving using Python Unit I 93
Program to demonstrate slicing of Strings (CO1)
# String slicing
String =ASTRING
# Using slice constructor
s1 = slice(3)
s2 = slice(1, 5, 2)
s3 = slice(-1, -12, -2)
print("String slicing")
print(String[s1])
print(String[s2])
print(String[s3])
2/15/2022 Sachin Kumar Problem Solving using Python Unit I 94
Output of slicing of Strings (CO1)
Output:
String slicing
AST
SR
GITA
• The plus (+) sign is the list concatenation operator, and the asterisk
(*) is the repetition operator.
count(arg), index(arg),
reverse(), sort(reverse=T/F)
update()
clear(), copy(),
keys(), values()
fromkeys(),
get(),
items(), pop(), popitem(),
setdefault(), update(),
• Relational Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Example
Operator Description
a=5, b=3
If the values of two operands are equal, then the condition a == b is not
==
becomes true true.
If values of two operands are not equal, then condition a!=b is true
!=
becomes true.
If the value of left operand is greater than the value of right a > b is true
>
operand, then condition becomes true.
If the value of left operand is less than the value of right a < b is not
<
operand, then condition becomes true. true
If the value of left operand is greater than or equal to the a >= b is
>=
value of right operand, then condition becomes true. true
If the value of left operand is less than or equal to the value a <= b is not
<=
of right operand, then condition becomes true. true
2/15/2022 Sachin Kumar Problem Solving using Python Unit I 113
Assignment Operators (CO1)
Operator Description
a=b
=
Assigns values from right side operands(b) to left side operand (a)
a+=b is same as a = a + b
+=
It adds right operand to the left operand and assign the result to left operand
a-=b is same as a = a - b
-=
It subtracts right operand from the left operand and assign the result to left operand
a*=b is same as a = a * b
*=
It multiplies right operand with the left operand and assign the result to left operand
a/=b is same as a = a / b
/=
It divides left operand with the right operand and assign the result to left operand
Operator Description
a%=b is same as a = a % b
%=
It takes modulus using two operands and assign the result to left operand
a**=b is same as a = a ** b
**= Performs exponential (power) calculation on operators and assign value to the
left operand
a//=b is same as a = a //b
//=
It performs floor division on operators and assign value to the left operand
Operator Description
a b a and b a or b not a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
Operator Description
() Parenthesis
** Exponentiation
~,+,- Unary operators
*,/,//,% Arithmetic multiply, division, floor and
modulo division
+, - Addition and subtraction
>>,<< Bitwise left and right shift operator
& Bitwise and operator
^ Bitwise Ex-or operator
| Bitwise or operator
2/15/2022 Sachin Kumar Problem Solving using Python Unit I 125
Operator Precedence and associativity(CO1)
Operator Description
<=,>=,<,> Relational inequality operators
==, != Equal and not equal operators
=,*=,/=,//=,%=,+=,-=,**=,&= Assignment operators
is, is not Identity operators
in, not in Membership operators
not Logical not operator
and Logical and operator
or Logical or operator
-9+9-8*6%8-int(19//10/2)|12&14
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = car("Toyota", 2016)
c1.display()
Toyota 2016
ID: 101
Name: John
ID: 102
Name: David
2. _________Operator
* is used to multiply numbers.
19. Write short notes with example: The Programming cycle for
Python, Elements of Python, Type conversion in Python,
operator precedence and Boolean expression.