Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Introduction to Python Programming-Theory

Theory (1030106502)

BHAGWAN MAHAVIR UNIVERSITY


BHAGWAN MAHAVIR POLYTECHNIC
COMPUTER ENGINEERING DEPARTMENT
THEORY NOTES
Introduction to Python Programming
Programming-Theory
Theory (1030106502)
Chapter 4: Decision Making and Loops
Python Input and Output Statements:
Input: Any information or data sent to the computer from the user through the keyboard is
called input.

Output: The information produced by the computer to the user is called output.

Python provides us with the two inbuilt functions as input() and output().
The syntax for input is input (prompt_message); the python will automatically identify
whether the user entered a string, number, or list; if the input entered from the user is not
correct, then python will throw a syntax error.

And the syntax for the out


output
put in python is print(), normally used to print the output.

Example:

x=int(input("Enter Fisrt number:"))


y=int(input("Enter Second Number:"))
print("The Sum:", i+j)

Command Line Arguments:


When we are executing the python program in the command line, we have to pass
python filename.py. Here along with the file name, we can pass different values to the
program, so these values are called command
command-line arguments.

>python filename.py value1 value2 ..


>python add.py 10 20
add.py is the file name (command lline argument)
10 is the value(command line argument)
20 is the value(command line argument)

Control Statements

If Statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if if-block.
ck. The condition of if statement can be any valid
logical expression which can be either evaluated to true or false.

Notes By: Bhavini Patel Page 1 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

The syntax of the if-statement


statement is given below.

if expression:
statement

Example: Program to print the largest of the three numbers.

a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");

Output:

Enter a? 100
Enter b? 120
Enter c? 130
c is largest

The if-else statement

The if-else
else statement provides an else block combined with the if statement which is
executed in the false case of the condition.

If the condition is true, then the if


if-block
block is executed. Otherwise, the else-block
else is
executed.

The syntax of the if-else


else statement is given below.

if condition:
#block of statements
else:
#another block of statements (else
(else-block)

Example: Program to check whether a number is even or not.

num = int(input("enter the number?"))


if num%2 == 0:
print("Number is even...")
else:
print("Number is odd...")

Notes By: Bhavini Patel Page 2 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

Output:

enter the number?10


Number is even

The elif statement


The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them. We can have any
number of elif statements in our program depending upon our need. However, using elif is
optional.

The elif statement works like an if


if-else-if
if ladder statement in C. It must be succeeded
by an if statement.

The syntax of the elif statement is given below.

if expression 1:
# block of statements

elif expression 2:
# block of statements

elif expression 3:
# block of statements

else:
# block of statements

Example:

number = int(input("Enter the number?"))


if number==10:
print("number is equals to 10")
elif number==50:
print("number is equal to 50");
elif number==100:
print("number is equal to 100");
else:
print("number is not equal to 10, 50 or 100")

Output:

Enter the number?15


number is not equal to 10, 50 or 100

Nested-if Statement
We can have an if…elif…else statement inside another if…elif…else statement. This
is called nesting in computer programming. Any number of these statements can be nested

Notes By: Bhavini Patel Page 3 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

inside one another. Indentation is the only way to figure out the level of nesting. Th
This can get
confusing, so it must be avoided if we can.

The syntax of the nested if...elif...else construct may be −

if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)

Example:

var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"

print "Good bye!"

Output:

Expression value is less than 200


Which is 100
Good bye!

Looping statements

The for Loop


Python's for loop is designed to repeatedly execute a code block while iterating
through a list, tuple, dictionary, or other iterable objects of Python. The process of traversing
a sequence is known as iteration.

Notes By: Bhavini Patel Page 4 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

A for loop is used for iterating over a sequence (that is a list, a tuple, a dictionary, a
set, or a string).

This is less like the for keyword in other programming languages, and works more
like an iterator method as found in other object
object-orientated
orientated programming languages.

Syntax of the for Loop

for value in sequence:


{ code block }

In this case, the variable


riable value is used to hold the value of every item present in the sequence
before the iteration begins until this particular iteration is completed.

Loop iterates until the final item of the sequence are reached.

Example:

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)

Output:

apple
banana
cherry

While Loop
While loops are used in Python to iterate until a specified condition is met. However,
the statement in the program that follows the while loop is executed once the conditi
condition
changes to false.

Syntax of the while loop is:

while <condition>:
{ code block }

Example:

# Python program to show how to use a while loop


counter = 0
# Initiating the loop
while counter < 10: # giving the condition
counter = counter + 3
print("Python Loops")

Notes By: Bhavini Patel Page 5 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

Output:

Python Loops
Python Loops
Python Loops
Python Loops

Nested Loops
If we have a piece of script that we want to run a number of times and then another
piece of script inside that script that we want to run B number of times, we employ a "nested
loop." When working with an iterable in the lists, these are widely utilized in Python.

Example:

adj = ["red", "big", "tasty"]


fruits = ["apple", "banana", "cherry"]

for x in adj:
for y in fruits:
print(x, y)

Output:

red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

Python break statement


The break is a keyword in python which is used to bring the program control out oof
the loop. The break statement breaks the loops one by one, i.e., in the case of nested loops, it
breaks the inner loop first and then proceeds to outer loops. In other words, we can say that
break is used to abort the current execution of the program and the control goes to the next
line after the loop.

The break is commonly used in the cases where we need to break the loop for a given
condition.

The syntax of the break is given below.

#loop statements
break;

Notes By: Bhavini Patel Page 6 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

Example:

for i in range(9):
if i > 3:
break
print(i)

Output:

0
1
2
3

The continue Keyword


In Python, the continue keyword return control of the iteration to the beginning of the
Python for loop or Python while loop. All remaining lines in the prevailing iteration of the
loop
oop are skipped by the continue keyword, which returns execution to the beginning of the
next iteration of the loop.

Both Python while and Python for loops can leverage the continue statements.

Example:

# Python code to show example of continue statem


statement

# looping from 10 to 20
for iterator in range(10, 21):

# If iterator is equals to 15, loop will continue to the next iteration


if iterator == 15:
continue
# otherwise printing the value of iterator
print( iterator )

Output:

10
11
12
13
14
16
17
18
19
20

Notes By: Bhavini Patel Page 7 of 8


Introduction to Python Programming-Theory
Theory (1030106502)

The Pass Statement

The pass statement is used as a placeholder for future code.

When the pass statement is executed, nothing happens, but you avoid getting an error
when empty code is not allowed.

Empty code is not allowed in loops, function definitions, class definitions, or in if


statements.

Example:

for x in [0, 1, 2]:


pass

Notes By: Bhavini Patel Page 8 of 8

You might also like