Lab 04
Lab 04
LAB MANUAL – 04
Course Instructor:
Lab Objective:
● To familiarize the students with basic programming concepts.
Hardware/Software required:
Hardware: Desktop/ Notebook Computer
Software Tool: Online Notebook
Lab Description:
Interactive Python:
If you execute Python from the command line with no script, Python gives you an interactive
prompt. This is an excellent facility for learning Python and for trying small snippets of code.
Many of the examples that follow were developed using the Python interactive prompt.
In addition, there are tools that will give you a more powerful and fancy Python interactive
mode. One example is IPython, which is available at http://ipython.scipy.org/. You may also
want to consider using IDLE. IDLE is a graphical integrated development environment for
Python. It contains a Python shell which helps you code in python by Auto-indent, Debugging
and Color-coding your program
1. Installing Python:
Download Python 3.6.2 for Windows from the following link:
https://www.python.org/downloads/
Install the downloaded executable file.
2. Opening IDLE
After installing Python, go to the start menu and search Python. Run the program labeled as
Integrated Development Environment (IDLE).
Mathematical Operations in Python:
>>> 1 + 1
2
>>> 6-5
1
>>> 2*5
10
>>> 5**2
25
>>> 21/3
7
>>> 23/3
7
>>> 23.0/3.0
7.6666...
>>> 23%3
2
Operators in Python
+ Addition 4+5 9
- Subtraction 8-5 3
* Multiplication 4*5 20
/ Division 19/3 6
% Remainder 19%3 5
** Exponent 2**4 16
Operator Precedence in Python:
Operators Descriptions
() Parentheses
+ - Addition, Subtraction
Comments in Python:
>>> #Commented Code
Variables:
Variables are containers for storing data values. In Python, it is not needed to declare variables
by specific data type. The Data type of a variable is decided at run-time.
Expression Function
!= not equal to
== equal to
Conditional Statements:
‘if - else' - Statement
a=1
if a > 5:
print ("This shouldn't happen.") #condition true
else:
print ("This should happen.") #condition false
‘elif' - Statement
z=4
if z > 70:
print ("Something is very wrong")
elif z < 7:
print ("This is normal")
Typecasting input
# type cast into integer
input_a = int(input_a)
String Properties:
len(string) - number of characters in a string (including spaces)
str.lower(string) - lowercase version of a string
str.upper(string) - uppercase version of a string
Example:
name = "Linkin Park"
length = len(name)
big_name = str.upper(name)
print (big_name, "has", length, "characters")
Output:
LINKIN PARK has 11 characters
Loops in Python:
'while' loop
a=0
while a < 10:
a=a+1
print (a)
'for' loop
for i in range(1, 5):
print (i)
for i in range(1, 5):
print (i)
else:
print ('The for loop is over')
Lab Tasks:
1. Write a python code to display your name, roll no, age, etc on output.
2. Modify task01 and take input from user in the variables name, roll no, and age and
display on output.
3. Write a python code to check whether a number is even or odd.