Obtaining user input in Python
Taking input is a way of interact with users, or get data to provide
some result. Python provides two built-in methods to read the data
from the keyboard. These methods are given below.
o input(prompt)
input()
The input function is used in all latest version of the Python. It takes
the input from the user and then evaluates the expression.
The Python interpreter automatically identifies the whether a user
input a string, a number, or a list. Let's understand the following
example.
Example -
# Python program showing
# a use of input()
name = input("Enter your name: ")
print(name)
Output:
Enter your name: Devansh
Devansh
The Python interpreter will not execute further lines until the user
enters the input.
Example - 2
# Python program showing
# a use of input()
name = input("Enter your name: ") # String Input
age = int(input("Enter your age: ")) # Integer Input
marks = float(input("Enter your marks: ")) # Float Input
print("The name is:", name)
print("The age is:", age)
print("The marks is:", marks)
Output:
Enter your name: Johnson
Enter your age: 21
Enter your marks: 89
The name is: Johnson
The age is 21
The marks is: 89.0
Explanation:
By default, the input() function takes input as a string so if we need to
enter the integer or float type input then the input() function must be
type casted. In the above code we type casted the user input
into int and float.
age = int(input("Enter your age: ")) # Integer Input
marks = float(input("Enter your marks: ")) # Float Input
How input() function works?
o The flow of the program has stopped until the user enters the
input.
o The text statement which also knows as prompt is optional to
write in input() function. This prompt will display the message
on the console.
o The input() function automatically converts the user input into
string. We need to explicitly convert the input using the type
casting.
Python Data Types
A variable can contain a variety of values. for example a person's id
must be stored as an integer, while their name must be stored as a
string.
The storage method for each of the standard data types that Python
provides is specified by Python. The following is a list of the Python-
defined data types.
Numbers
Numeric values are stored in numbers. The whole number, float, and
complex qualities have a place with a Python Numbers datatype.
Python offers the type() function to determine a variable's data type.
When a number is assigned to a variable, Python generates Number
objects.
Example:
a=5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
Output:
The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
Python supports three kinds of numerical data.
o Int: Whole number worth can be any length, like numbers 10, 2,
29, - 20, - 150, and so on. An integer can be any length you want
in Python. Its worth has a place with int.
o Float: Float stores drifting point numbers like 1.9, 9.902, 15.2, etc.
It can be accurate to within 15 decimal places.
o Complex: An intricate number contains an arranged pair, i.e., x +
iy, where x and y signify the genuine and non-existent parts
separately. The complex numbers like 2.14j, 2.0 + 2.3j, etc.
Sequence Type
String
The sequence of characters in the quotation marks can be used to
describe the string. A string can be defined in Python using single,
double, or triple quotes.
String dealing with Python is a direct undertaking since Python gives
worked-in capabilities and administrators to perform tasks in the
string.
When dealing with strings, the operation "hello"+" python" returns
"hello python," and the operator + is used to combine two strings.
Because the operation "Python" *2 returns "Python," the operator * is
referred to as a repetition operator.
The Python string is demonstrated in the following example.
Example - 1
str = "string using double quotes"
print(str)
s = '''''A multiline
string'''
print(s)
Output:
string using double quotes
A multiline
string
Example - 2
str1 = 'hello java' #string str1
str2 = ' how are you' #string str2
print (str1[0:2]) #printing first two character using slice operator
print (str1[4]) #printing 4th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
Output:
he
o
hello java hello java
hello javat how are you
List
Lists in Python are like arrays in C, but lists can contain data of
different types. The things put away in the rundown are isolated with a
comma (,) and encased inside square sections [].
To gain access to the list's data, we can use slice [:] operators. Like how
they worked with strings, the list is handled by the concatenation
operator (+) and the repetition operator (*).
Example:
list1 = [1, "hi", "Python", 2]
#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
# List slicing
print (list1[0:2])
# List Concatenation using + operator
print (list1 + list1)
# List repetation using * operator
print (list1 * 3)
Output:
[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
Tuple
In many ways, a tuple is like a list. Tuples, like lists, also contain a
collection of items from various data types. A parenthetical space ()
separates the tuple's components from one another.
Because we cannot alter the size or value of the items in a tuple, it is a
read-only data structure.
Let's look at a straightforward tuple in action.
Example:
tup = ("hi", "Python", 2)
# Checking type of tup
print (type(tup))
#Printing the tuple
print (tup)
# Tuple slicing
print (tup[1:])
print (tup[0:1])
# Tuple concatenation using + operator
print (tup + tup)
# Tuple repatation using * operator
print (tup * 3)
# Adding value to tup. It will throw an error.
t[2] = "hi"
Output:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Traceback (most recent call last):
File "main.py", line 14, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment
Dictionary
A dictionary is a key-value pair set arranged in any order. It stores a
specific value for each key, like an associative array or a hash table.
Value is any Python object, while the key can hold any primitive data
type.
The comma (,) and the curly braces are used to separate the items in
the dictionary.
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
# Printing dictionary
print (d)
# Accesing value using keys
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print (d.keys())
print (d.values())
Output:
1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
Boolean
True and False are the two default values for the Boolean type. These
qualities are utilized to decide the given assertion valid or misleading.
The class book indicates this. False can be represented by the 0 or the
letter "F," while true can be represented by any value that is not zero.
# Python program to check the boolean type
print(type(True))
print(type(False))
Output:
<class 'bool'>
<class 'bool'>
Set
The data type's unordered collection is Python Set. It is iterable,
mutable(can change after creation), and has remarkable components.
The elements of a set have no set order; It might return the element's
altered sequence. Either a sequence of elements is passed through the
curly braces and separated by a comma to create the set or the built-
in function set() is used to create the set. It can contain different kinds
of values.
# Creating Empty set
set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)
Output:
{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}
Python Operators
Introduction:
In this article, we are discussing Python Operators. The operator is a
symbol that performs a specific operation between two operands,
according to one definition. Operators serve as the foundation upon
which logic is constructed in a program in a particular programming
language. In every programming language, some operators perform
several tasks. Same as other languages, Python also has some
operators, and these are given below -
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators
o Arithmetic Operators
Arithmetic Operators
Arithmetic operators used between two operands for a particular
operation. There are many arithmetic operators. It includes the
exponent (**) operator as well as the + (addition), - (subtraction), *
(multiplication), / (divide), % (reminder), and // (floor division)
operators.
Consider the following table for a detailed explanation of arithmetic
operators.
Operator Description
+ (Addition) It is used to add two operands. For example, if a =
10, b = 10 => a+b = 20
- (Subtraction) It is used to subtract the second operand from the
first operand. If the first operand is less than the
second operand, the value results negative. For
example, if a = 20, b = 5 => a - b = 15
/ (divide) It returns the quotient after dividing the first
operand by the second operand. For example, if a =
20, b = 10 => a/b = 2.0
* It is used to multiply one operand with the other. For
(Multiplication) example, if a = 20, b = 4 => a * b = 80
% (reminder) It returns the reminder after dividing the first
operand by the second operand. For example, if a =
20, b = 10 => a%b = 0
** (Exponent) As it calculates the first operand's power to the
second operand, it is an exponent operator.
// (Floor It provides the quotient's floor value, which is
division) obtained by dividing the two operands.
Program Code:
a = 32 # Initialize the value of a
b=6 # Initialize the value of b
print('Addition of two numbers:',a+b)
print('Subtraction of two numbers:',a-b)
print('Multiplication of two numbers:',a*b)
print('Division of two numbers:',a/b)
print('Reminder of two numbers:',a%b)
print('Exponent of two numbers:',a**b)
print('Floor division of two numbers:',a//b)
Output:
Addition of two numbers: 38
Subtraction of two numbers: 26
Multiplication of two numbers: 192
Division of two numbers: 5.333333333333333
Reminder of two numbers: 2
Exponent of two numbers: 1073741824
Floor division of two numbers: 5
Comparison operator
Comparison operators mainly use for comparison purposes.
Comparison operators compare the values of the two operands and
return a true or false Boolean value in accordance. The example of
comparison operators are ==, !=, <=, >=, >, <. In the below table, we
explain the works of the operators.
Operator Description
== If the value of two operands is equal, then the condition
becomes true.
!= If the value of two operands is not equal, then the
condition becomes true.
<= The condition is met if the first operand is smaller than or
equal to the second operand.
>= The condition is met if the first operand is greater than or
equal to the second operand.
> If the first operand is greater than the second operand,
then the condition becomes true.
< If the first operand is less than the second operand, then
the condition becomes true.
Program Code:
a = 32 # Initialize the value of a
b=6 # Initialize the value of b
print('Two numbers are equal or not:',a==b)
print('Two numbers are not equal or not:',a!=b)
print('a is less than or equal to b:',a<=b)
print('a is greater than or equal to b:',a>=b)
print('a is greater b:',a>b)
print('a is less than b:',a<b)
Output:
Two numbers are equal or not: False
Two numbers are not equal or not: True
a is less than or equal to b: False
a is greater than or equal to b: True
a is greater b: True
a is less than b: False
Assignment Operators
Using the assignment operators, the right expression's value is
assigned to the left operand. There are some examples of assignment
operators like =, +=, -=, *=, %=, **=, //=. In the below table, we
explain the works of the operators.
Operator Description
= It assigns the value of the right expression to the left
operand.
+= By multiplying the value of the right operand by the value
of the left operand, the left operand receives a changed
value. For example, if a = 10, b = 20 => a+ = b will be
equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of
the right operand and assigns the modified value back to
left operand. For example, if a = 20, b = 10 => a- = b will
be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of
the right operand and assigns the modified value back to
then the left operand. For example, if a = 10, b = 20 => a*
= b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the
right operand and assigns the reminder back to the left
operand. For example, if a = 20, b = 10 => a % = b will be
equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2,
a**=b will assign 4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b =
3, a//=b will assign 4//3 = 1 to a.
Program Code:
Now we give code examples of Assignment operators in Python. The
code is given below -
a = 32 # Initialize the value of a
b=6 # Initialize the value of b
print('a=b:', a==b)
print('a+=b:', a+b)
print('a-=b:', a-b)
print('a*=b:', a*b)
print('a%=b:', a%b)
print('a**=b:', a**b)
print('a//=b:', a//b)
Output:
a=b: False
a+=b: 38
a-=b: 26
a*=b: 192
a%=b: 2
a**=b: 1073741824
a//=b: 5
Bitwise Operators
The two operands' values are processed bit by bit by the bitwise
operators. The examples of Bitwise operators are bitwise OR (|), bitwise
AND (&), bitwise XOR (^), negation (~), Left shift (<<), and Right shift
(>>). Consider the case below.
For example,
if a = 7
b=6
then, binary (a) = 0111
binary (b) = 0110
hence, a & b = 0011
a | b = 0111
a ^ b = 0100
~ a = 1000
Let, Binary of x = 0101
Binary of y = 1000
Bitwise OR = 1101
8421
1 1 0 1 = 8 + 4 + 1 = 13
Bitwise AND = 0000
0000 = 0
Bitwise XOR = 1101
8421
1 1 0 1 = 8 + 4 + 1 = 13
Negation of x = ~x = (-x) - 1 = (-5) - 1 = -6
~x = -6
In the below table, we are explaining the works of the bitwise
operators.
Operator Description
& (binary A 1 is copied to the result if both bits in two operands at
and) the same location are 1. If not, 0 is copied.
| (binary The resulting bit will be 0 if both the bits are zero;
or) otherwise, the resulting bit will be 1.
^ (binary If the two bits are different, the outcome bit will be 1, else
xor) it will be 0.
~ The operand's bits are calculated as their negations, so if
(negation) one bit is 0, the next bit will be 1, and vice versa.
<< (left The number of bits in the right operand is multiplied by
shift) the leftward shift of the value of the left operand.
>> (right The left operand is moved right by the number of bits
shift) present in the right operand.
Program Code:
a=5 # initialize the value of a
b=6 # initialize the value of b
print('a&b:', a&b)
print('a|b:', a|b)
print('a^b:', a^b)
print('~a:', ~a)
print('a<<b:', a<<b)
print('a>>b:', a>>b)
Output:
a&b: 4
a|b: 7
a^b: 3
~a: -6
a<>b: 0
Logical Operators
The assessment of expressions to make decisions typically uses logical
operators. The examples of logical operators are and, or, and not. In
the case of logical AND, if the first one is 0, it does not depend upon
the second one. In the case of logical OR, if the first one is 1, it does
not depend on the second one. Python supports the following logical
operators. In the below table, we explain the works of the logical
operators.
Operator Description
and The condition will also be true if the expression is true. If
the two expressions a and b are the same, then a and b
must both be true.
or The condition will be true if one of the phrases is true. If a
and b are the two expressions, then an or b must be true if
and is true and b is false.
not If an expression a is true, then not (a) will be false and vice
versa.
Program Code:
a=5 # initialize the value of a
print(Is this statement true?:',a > 3 and a < 5)
print('Any one statement is true?:',a > 3 or a < 5)
print('Each statement is true then return False and vice-
versa:',(not(a > 3 and a < 5)))
Output:
Is this statement true?: False
Any one statement is true?: True
Each statement is true then return False and vice-versa: True
Membership Operators
The membership of a value inside a Python data structure can be
verified using Python membership operators. The result is true if the
value is in the data structure; otherwise, it returns false.
Operator Description
in If the first operand cannot be found in the second
operand, it is evaluated to be true (list, tuple, or
dictionary).
not in If the first operand is not present in the second operand,
the evaluation is true (list, tuple, or dictionary).
Program Code:
x = ["Rose", "Lotus"]
print(' Is value Present?', "Rose" in x)
print(' Is value not Present?', "Riya" not in x)
Output:
Is value Present? True
Is value not Present? True
Identity Operators
Operator Description
is If the references on both sides point to the same object, it
is determined to be true.
is not If the references on both sides do not point at the same
object, it is determined to be true.
Program Code:
a = ["Rose", "Lotus"]
b = ["Rose", "Lotus"]
c=a
print(a is c)
print(a is not c)
print(a is b)
print(a is not b)
print(a == b)
print(a != b)
Output:
True
False
False
True
True
False
Python If-else statements
Decision making is the most important aspect of almost all the
programming languages. As the name implies, decision making allows
us to run a particular block of code for a particular decision. Here, the
decisions are made on the validity of the particular conditions.
Condition checking is the backbone of decision making.
In python, decision making is performed by the following statements.
Statement Description
If The if statement is used to test a specific condition. If the
Statement condition is true, a block of code (if-block) will be
executed.
If - else The if-else statement is similar to if statement except the
Statement fact that, it also provides the block of the code for the
false case of the condition to be checked. If the condition
provided in the if statement is false, then the else
statement will be executed.
Nested if Nested if statements enable us to use if ? else statement
Statement inside an outer if statement.
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't
allow the use of parentheses for the block level code. In Python,
indentation is used to declare a block. If two statements are at the
same indentation level, then they are the part of the same block.
Generally, four spaces are given to indent the statements which are a
typical amount of indentation in python.
Indentation is the most used part of the python language since it
declares the block of code. All the statements of one block are
intended at the same level indentation. We will see how the actual
indentation takes place in decision making and other stuff in python.
The 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-block. The
condition of if statement can be any valid logical expression which can
be either evaluated to true or false.
The syntax of the if-statement is given below.
if expression:
statement
Example 1
# Simple Python program to understand the if statement
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamica
lly
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, w
e will enter the block
print("The Given number is an even number")
Output:
enter the number: 10
The Given number is an even number
Example 2 : Program to print the largest of the three
numbers.
# Simple Python Program to print the largest of the three number
s.
a = int (input("Enter a: "));
b = int (input("Enter b: "));
c = int (input("Enter c: "));
if a>b and a>c:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print ("From the above three numbers given a is largest");
if b>a and b>c:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print ("From the above three numbers given b is largest");
if c>a and c>b:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print ("From the above three numbers given c is largest");
Output:
Enter a: 100
Enter b: 120
Enter c: 130
From the above three numbers given c is largest
The if-else statement
The if-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-block is executed. Otherwise, the
else-block is executed.
The syntax of the if-else statement is given below.
if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person is
eligible to vote or not.
# Simple Python Program to check whether a person is eligible t
o vote or not.
age = int (input("Enter your age: "))
# Here, we are taking an integer num and taking input dynamical
ly
if age>=18:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
Output:
Enter your age: 90
You are eligible to vote !!
Example 2: Program to check whether a number is
even or not.
# Simple Python Program to check whether a number is even or
not.
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamical
ly
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The Given number is an even number")
else:
print("The Given Number is an odd number")
Output:
enter the number: 10
The Given number is even number
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-else-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 1
# Simple Python program to understand elif statement
number = int(input("Enter the number?"))
# Here, we are taking an integer number and taking input dynam
ically
if number==10:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The given number is equals to 10")
elif number==50:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The given number is equal to 50");
elif number==100:
# Here, we are checking the condition. If the condition is true, we
will enter the block
print("The given number is equal to 100");
else:
print("The given number is not equal to 10, 50 or 100");
Output:
Enter the number?15
The given number is not equal to 10, 50 or 100
Example 2
# Simple Python program to understand elif statement
marks = int(input("Enter the marks? "))
# Here, we are taking an integer marks and taking input dynami
cally
if marks > 85 and marks <= 100:
# Here, we are checking the condition. If the condition is true, w
e will enter the block
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
# Here, we are checking the condition. If the condition is true, w
e will enter the block
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
# Here, we are checking the condition. If the condition is true, w
e will enter the block
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
# Here, we are checking the condition. If the condition is true, w
e will enter the block
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
Output:
Enter the marks? 89
Congrats ! you scored grade A ...
Python Loops
The following loops are available in Python to fulfil the looping needs.
Python offers 3 choices for running the loops. The basic functionality
of all the techniques is the same, although the syntax and the amount
of time required for checking the condition differ.
We can run a single statement or set of statements repeatedly using a
loop command.
The following loops are available in the Python programming
language.
Sr. Name of Loop Type & Description
No. the loop
1 While Repeats a statement or group of statements while a
loop given condition is TRUE. It tests the condition
before executing the loop body.
2 For loop This type of loop executes a code block multiple
times and abbreviates the code that manages the
loop variable.
3 Nested We can iterate a loop inside another loop.
loops
Loop Control Statements
Statements used to control loops and change the course of iteration
are called control statements. All the objects produced within the local
scope of the loop are deleted when execution is completed.
Python provides the following control statements. We will discuss
them later in detail.
Sr.No. Name of Description
the
control
statement
1 Break This command terminates the loop's execution
statement and transfers the program's control to the
statement next to the loop.
2 Continue This command skips the current iteration of the
statement loop. The statements following the continue
statement are not executed once the Python
interpreter reaches the continue statement.
3 Pass The pass statement is used when a statement is
statement syntactically necessary, but no code is to be
executed.
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.
Syntax of the for Loop
for value in sequence:
{ code block }
In this case, the variable 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.
Code
# Python program to show how the for loop works
# Creating a sequence which is a tuple of numbers
numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2]
# variable to store the square of the number
square = 0
# Creating an empty list
squares = []
# Creating a for loop
for value in numbers:
square = value ** 2
squares.append(square)
print("The list of squares is", squares)
Output:
The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81, 4]
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 condition changes to false.
Syntax of the while loop is:
while <condition>:
{ code block }
All the coding statements that follow a structural command define a
code block. These statements are intended with the same number of
spaces. Python groups statements together with indentation.
Code
# 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")
Output:
Python Loops
Python Loops
Python Loops
Python Loops
Loop Control Statements
Continue Statement
It returns the control to the beginning of the loop.
Code
# Python program to show how the continue statement works
# Initiating the loop
for string in "Python Loops":
if string == "o" or string == "p" or string == "t":
continue
print('Current Letter:', string)
Output:
Current Letter: P
Current Letter: y
Current Letter: h
Current Letter: n
Current Letter:
Current Letter: L
Current Letter: s
Break Statement
It stops the execution of the loop when the break statement is
reached.
Code
# Python program to show how the break statement works
# Initiating the loop
for string in "Python Loops":
if string == 'L':
break
print('Current Letter: ', string)
Output:
Current Letter: P
Current Letter: y
Current Letter: t
Current Letter: h
Current Letter: o
Current Letter: n
Current Letter:
Pass Statement
Pass statements are used to create empty loops. Pass statement is also
employed for classes, functions, and empty control statements.
Code
# Python program to show how the pass statement works
for a string in "Python Loops":
pass
print( 'Last Letter:', string)
Output:
Last Letter: s