Experiment - 4
Experiment - 4
Problem Statement:
1) Write a Python program that accepts a word from the user and reverse it.
2) Write a Python program to count the number of even and odd numbers from a series of numbers.
Outcomes: Students will be able to learn to create basic python sketch Prerequisite:
Hardware requirements:
Processors: Intel Atom® processor or Intel® Core™ i3 processor.
Disk space: 1 GB
Software requirements:
Operating systems: Windows* 7 or later, macOS, and Linux.
Python versions: 2.7.X, 3.6.X. , Jupyter Notebook
Theory:
Decision making is the most important aspect of almost all 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.
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.
1. if expression:
2. 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.
1. if condition:
2. #block of statements
3. else:
4. #another block of statements (else-block)
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.
1. if expression 1:
2. # block of statements
3.
4. elif expression 2:
5. # block of statements
6.
7. elif expression 3:
8. # block of statements
9.
10. else:
11. # block of statements
Instructions: N.A.
2) Write a Python program to count the number of even and odd numbers from a series of
numbers.
Output of count the number of even and odd numbers from a series of numbers :
Sample Viva Questions and Answers:
Ans : Python is a dynamically typed language. We don't have to declare the type of
a variable or manage the memory while assigning a value to a variable in Python.
Other languages like C, C++, Java, etc.., there is a strict declaration of
variables before assigning values to them.
Python doesn't have any problem even if we don't declare the type of variable.
It states the kind of variable in the runtime of the program. So, Python is a
dynamically typed language.
Ans : The pass statement is a null operation; nothing happens when it executes. The pass
is also useful in places where your code will eventually go, but has not been written.
Ans : Python program runs directly from the source code. - Each time Python programs are
executed code is required. - Python converts source code written by the programmer
into intermediate language which is again translated into the native language / machine
language that is executed.
4. Is Python case-sensitive?
Ans : The Python language is case-sensitive. Identifiers, keywords, and names of classes must
be written in lowercase letters. However, Python allows developers to write in uppercase
letters for constants (i.e. variables that have a value assigned to them during program
execution), and for built-in functions, and for class and module names, etc.
Syntax :
[on_true] if [expression] else [on_false]