Ppt-Unit II
Ppt-Unit II
Ppt-Unit II
Basic loop Structures/Iterative statements: while loop, for loop, selecting appropriate
loop. Nested loops, The break, continue, pass, else statement used with loops.
Numeric - This data type includes integers (e.g. 11,121 etc.), float (e.g. 23.222,343.2234 etc.) &
complex (e.g. 2+2i, 5+2323j etc.).
(i) integer
int
var2 = 1231564 #var2 is the variable, whereas, 1231564 is the value stored
sum=var1+var2
Output:
decimal values
Program 2.3:
sum=var1+var2
Output:
Output:
Addition = (3+6j)
String
store the string literals (e.g. “Hello” “etc” or ‘Hi’, ‘Bye’ etc.)
print(var3)
Output:
Hello
var1=1231564
var2=11.23
var3=”Hello”
print(type(var1))
print(type(var2))
print(type(var3))
Output:
<class ‘int’>
<class 'float'>
<class 'str'>
bool (Boolean)
if the user wants to evaluate logical operations & store the result in a variable for further
using it in the program execution?
If number a is greater than b or not i.e. a>b, it evaluates it to either True or False.
var4=15.1>29.8
print(type(var4))
Output:
Result is False
<class 'bool'>
Keywords in python
Why should we use Python language for writing this program, let us say, addition of 2
numbers as compared to machine and assembly language?
If you use machine language to write this program, you have to write
instructions in machine language which is difficult and error prone.
If you use assembly language to write this code, you would have to write
instructions in assembly language using mnemonics
Platform independent
Features of the Python
Structural & Object Oriented: Python supports sequential, selection & iterative control flow. It is
possible to create objects in Python. Hence, it is object oriented as well.
Platform independent
Simple: Python is a simple and easy to learn language because of its clear syntax and readability.
Secure: Python is secure because the reverse engineering of the .pyc files (byte code) is a lot harder.
Architectural- neutral: Python compiler generates an architecture-neutral byte code file which makes
the compiled code to be executable on many processors with the present Python runtime system.
Robust: Programmer has to write few lines of code as compared to other languages
Multi-threaded: With Python's multi-threaded feature it is possible to write
programs that can do many tasks simultaneously.
To make the program add.py more user friendly, you can change the print instruction as
below.
Whatever characters we write inside the quotes of the first parameter of the print function
assigned a particular address. Every variable has a name, a type, a size and a value. We can declare a
<variable_name> = <value>
● Whenever a new value is placed into a variable, it replaces (and destroys) the previous value.
a=10
Python does not allow special characters such as @, $, and % within identifiers.
Windows: https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe
Linux: https://www.python.org/downloads/source/
PhysicsMarks = int(input())
ChemistryMarks = int(input())
MathsMarks = int(input())
PCM_Average = (PhysicsMarks+ChemistryMarks+MathsMarks)/3
OR
1) Sequential: Statements in a program are executed one after another i.e. in a sequential manner.
4) Jump: Different set of instructions in the program are executed as per the jump defined
if (expression):
Statement1 #Statement associated with if part
else:
Statement2 #Statement associated with else part
If expression is true, statement1 is executed. Otherwise, statement2 is executed.
PhysicsMarks = int(input("Please enter your Physics marks: "))
if PhysicsMarks>MathematicsMarks :
else:
if (expression):
statement
PhysicsMarks = int(input("Please enter your Physics marks: "))
if PhysicsMarks==MathsMarks:
if PhysicsMarks==MathsMarks:
if PhysicsMarks>MathsMarks:
else:
if(expression1):
Statement1
elif(expression2):
Statement2
else:
Statements
PhysicsMarks = int(input("Please enter your Physics marks: "))
if PhysicsMarks==MathsMarks:
elif PhysicsMarks>MathsMarks:
else:
num = 12
diff = 5
num = num+diff
num = num+diff
num = num+diff
num = num+diff
`
while loop:
while loop executes a set of instruction(s) repetitively until the conditional expression is true.
while <expression>
statement(s)
count=0
while count<5:
count+=1
print (count)
exercise
var = 1 var = 1
print ("You entered: ", num) print ("You entered: ", num)
It is not practical to write a list of such 1000 numbers. Is there anyway by which
we can generate such list
automatically? Yes, Python provides in-built function range(start, stop, step) which
does it.
range(start, stop, step) where,
in sequence.
The complete program can be written as below by using for loop construct (for
loop):
num = 12
diff = 5
num = num+diff
When the break statement gets executed inside the loop, it immediately stops the
execution of the instruction(s) inside that loop and the next statement after the
loop gets executed.
Let’s understand this concept by a simple program that calculates the number of the digits present in each
number.
2. Assume the input number received is 8745. Divide the number by 10 and again divide the result of
v. No. of digits in the number = No. of times the division operation carried as above
num=int(input())
count=1
while 1:
if(num/10>1):
count+=1
num/=10
else:
break
When the continue statement gets executed inside the loop, the program control
gets immediately transferred to the conditional expression in case of while loop
and next value in the list gets assigned to a loop counter in case of a for loop.
If the conditional expression is true, the loop continues in a usual manner in case
of while loop, whereas, the loop continues until value is present in the sequence.
Let’s write a program that calculates the sum of numbers
starting from 0 till 20 and the number should not be
divisible by 2 or 3.
In this program, we need to modify repetitive statement
in such a manner that when any number that is
completely divisible by 2 or 3, it should not be added.
rem1= 0
rem2= 0
sum= 0
for i in range (0,21):
rem1=i%2
rem2=i%3
if (rem1==0 or rem2==0):
continue
sum+=i
print("The sum of all numbers starting from 0 to 20 and not divisible by 2 or 3 is ",sum)
Nested loops
Every loop has a repetitive statement. When any loop statement contains one or
more loops inside it, it is called as a nested loop.
Nested loops
Every loop has a repetitive statement. When any loop statement contains one or
more loops inside it, it is called as a nested loop.
Nested loops
Every loop has a repetitive statement. When any loop statement contains one or
more loops inside it, it is called as a nested loop.
ch = "*"
for i in range (1,11):
for j in range (0,i):
print(ch, end="")
print()
example
Practice Example 4: Write a program that generates following pattern using for loops. e.g. If the
input value is 5, it should generate a pattern as below.
1
12
123
1234
12345
1234
123
12
1
String
print("print String")
print("Hello")
Output: Hello
a = "Hello"
print(a)
Output: Hello
print("Multiline String")
Pune"""
print(a)
print("String indexing")
a = "Hello, World!"
print(a[1])
Output: e
print("String loop")
for x in "banana":
print(x)
print("String length")
a = "Hello, World!"
print(len(a))
Output: 13
—---------
print("String presence")
print("free" in txt)
Output: true
print("String presence")
if "free" in txt:
—---
b = "Hello, World!"
print(b[2:5])
Output:llo
—--
b = "Hello, World!"
print(b[:5])
Output: Hello
—--
b = "Hello, World!"
print(b[2:])
Output: llo,World!
print("Negative Indexing")
b = "Hello, World!"
print(b[-5:-2])
Output: orl
print("Upper case")
a = "Hello, World!"
print(a.upper())
print()
print("Lower case")
a = "Hello, World!"
print(a.lower())
print("Remove Whitespace")
—---------
print("Replace String")
a = "Hello, World!"
print(a.replace("H", "J"))
—----
print("Split String")
a = "Hello, World!"
age = 36
print(txt.format(age))
—------
quantity = 3
itemno = 567
price = 49.95
—--------
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print("The escape character allows you to use double quotes when you normally
would not be allowed")
print(txt)
String
String Methods