0% found this document useful (0 votes)
14 views

Python Revision Tour 2 - Class12

Uploaded by

niharika15012007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Revision Tour 2 - Class12

Uploaded by

niharika15012007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Python Revision Tour -2

Using Random module:


Random module used to provide random number generators. For this we need to import module random eg:
import random
3 common random functions in random module:
1. random() – return random floating point number range from (0.0 – 1.0)
eg: import random()
print(random.random())
0.0223345687(0.0 – 1.0)
2. randint(a,b) – returns integer range(a,b)
eg: print(random.random()*(35-15)+15)
28.3071872734
3. randrange(start,stop, step) – it returns random numbers with start …. stop with step value.
Eg: print(random.randint(15,35))
16
random.randrange(45)
13
random.randrange(11,45,4)
15
import random
print(random.randint(3,10)-3)
minimum possible number=0
maximum possible number =7
Using the statistics module:
Statistics module used to provide many statistics functions such as mean(), median(), mode(). Inorder to use
this we have to import statistics.
import statistics
or from statistics import mean, median, mode
statistics .mean(seq) – return the value of the set / seq
statistics. median(seq) – return the middle value of the set/ seq
statistics. Mode(seq) – return the most often repeated value of the set /seq passed.
import statistics
seq = [5,6,7,5,6,5,5,9,11,12,23,5]
statistics.mean(seq)
8.25
Statististics.median(seq)
6.0
Statististics . mode(seq)
5
Debugging:
 Debugging means to remove ‘bugs’ from a program.
 Debugging involves rectifying the code.
 Error sometimes called as bug.
 Error prevents the code from compiling and running.
There are 2 types of errors:
1. Syntax errors:
Syntax errors occurs when rules of a programming language are misused.
2. Semantics errors:
Semantics errors occurs when statements are not meaningful.
i)Logical errors:
If you don’t encounter any error during compile – time and run time , your program does not provide
the correct result. Because of programmer mistake analysis of the problem.
ii) Runtime errors:
errors occur during the execution of a program are runtime errors. these are harder to detect errors.
Some runtime error stop execution called ‘crashed’ or ‘abnormally terminated’.
Exceptions: errors and exceptions are similar but different terms.
 Errors represent any bug in the code.
 Exception refers to irregular situation occur during the execution/runtime.
Print statements in python:
Types of statements:
Statements are the instructions given to the computer to perform some action.
There are 3 types of statements:
1. Empty statements – pass.
2. Simple statements- single executable statements.
3. Compound statements – group of statements execute as a unit.
Flow of control:
Control statements are used to control the flow of execution depending upon the specific condition. The
control flow of a Python program is regulated by conditional statements, loops, and function calls.
1. Decision making statements
a. if statements
b. if-else statements
c. Nested if-else statement
2. Iteration statements (LOOPS)
a. While Loop
b. For Loop
c. Nested For Loops
3. Jump statements ( Break, continue, Pass )

If Statements : An if statement is a programming conditional statement that, if proved true, performs a


function or displays information.
Input Output

x=1 Matched
y=2
if(x==1 and y==2):
print(‘Matched')
if-else Statements

• If-else statement executes some code if the test expression is


true
and some other code if the test expression is false

Input Output

a=10 less than 100


if(a < 100):
print(‘less than 100')
else:
print(‘more than equal 100')
Nested if-else statement
The nested if...else statement allows you to check for multiple test expressions and execute different codes
for more than two conditions.
Input Output

num = float(input("Enter a number: ")) Enter a number: 4


if num >= 0: Positive number
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
if statements
Syntax:
if ( condition):
Statement(s)
Example:
age=int(input(“Enter Age: “))
if ( age>=18):
print(“You are eligible for vote”)
if(age<0):
print(“You entered Negative Number”)

Ladder if else statements (if-elif-else)


Syntax
if expression:
statement(s)
elif expression:
statement(s)
elif expression:
statement(s)
...
else:
statement(s)
Iteration Statements (Loops)
While Loop
It is used to execute a block of statement as long as a given condition is true. And when the condition become
false, the control will come out of the loop. The condition is checked every time at the beginning of the loop.
While – else
It is used to execute a block of statement as long as a given condition is true. And when the condition become
false, it execute else part of block of statements and come out of the loop.
Input (While ) Output
Input (While-else) Output
x=1 1 i=0 1
while (x <= 4): 2 while i < 4: 2
print(x) 3 i += 1 3
x=x+1 4 print(i) 4
else: Break
print("Break\n")

You might also like