Lab1
This lab is an introductory session on Python. It is a powerful object-oriented programming language, comparable to Perl, Ruby, Scheme and Java. Some of Python’s notable features are:
- Easy to use that makes it simple to get your first programs working
- Easy to learn that makes it an excellent choice for beginners
- Runs everywhere, including Mac OS X, Windows, Linux and Unix.
The lab will teach students to:
- Basic Operations on strings and numbers
- Basic use of conditionals
- Basic use of loops
Introduction
Programming is simply the act of entering instructions for the computer to perform. These instructions might crunch some numbers, modify text, look up information in files, or communicate with other computers over the Internet. All programs use basic instructions as building blocks. Here are a few of the most common ones, in English:
- “Do this; then do that.”
- “If this condition is true, perform this action; otherwise, do that action.”
- “Do this action that number of times.”
- “Keep doing that until this condition is true.”
You can combine these building blocks to implement more intricate decisions, too. Programming is a creative task, somewhat like constructing a castle out of LEGO bricks. You start with a basic idea of what you want your castle to look like and inventory your available blocks. Then you start building. Once you’ve finished building your program, you can pretty up your code just like you would your castle.
Python refers to the Python programming language (with syntax rules for writing what is considered valid Python code) and the Python interpreter software that reads source code (written in the Python language) and performs its instructions. The name Python comes from the surreal British comedy group Monty Python, not from the snake. Python programmers are affectionately called Pythonistas.
Lab Activities:
Activity 1:
Display numbers on screen using Python IDLE.
Solution:
- Run Python IDLE
- Type in any number, say 24 and press Enter.
- 24 should be printed on the screen
- Now type 4.2, press Enter.
- 4.2 should be displayed on screen as an output.
- Now type print(234). Press Enter. 234 will be the output.
- Type print(45.90) and press Enter. The output will show 45.90 on screen
Activity 2:
Display strings on screen.
Solution:
- Python recognized strings through quotes (single, double). Anything inside quotes is a string for Python interpreter.
- Type hello, press Enter. An error message will be displayed as Python interpreter does not understand this as a string.
- Type ‘hello’ and press Enter. Hello will be displayed.
- Type ‘Quote me on this!’ and press Enter. Same string will be displayed.
- Type “What’s your name?” and press Enter. What’s your name? will be printed on the screen.
- You can specify multi-line strings using triple quotes – “ “ “ or ‘ ‘ ‘. Type following text and press Enter

Activity 3:
Use Python as a calculator.
Solution:
- The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators + (addition), – (subtraction), * (multiplication) and / (division) work just like in most other languages. Parentheses (()) can be used for grouping.
- Type in 2 + 2 and press Enter. Python will output its result i.e. 4.
- Try following expressions in the shell.
a. 50 – 4
b. 23.5 – 2.0
c. 23 – 18.5
d. 5 * 6
e. 2.5 * 10
f. 2.5 * 2.5 - 28 / 4 (note: division returns a floating point number)
- 26 / 4
- 23.4 / 3.1
Activity 4:
Get an integer answer from division operation. Also get remainder of a division operation in the output.
Solution:
- Division (/) always gives a float answer. There are different ways to get a whole as an answer.
- Use // operator:
a. Type 28 // 4 and press Enter. The answer is a whole number.
b. Type 26 // 4 and press Enter. - Use (int) cast operator: this operator changes the interchangeable types.
a. Type (int)26 / 4 and press Enter. The answer is a whole number.
b. Type (int) 28/4; press Enter. - The modulus (or mod) % operator is used to get the remainder as an output (division / operator returns the quotient).
a. Type 28 % 4. Press Enter. 0 will be the result.
b. Type 26 % 4. Press Enter. 2 will be the result
Activity 5:
Calculate 43, 410, 429, 4150, 41000
Solution:
- The multiplication (*) operator can be used for calculating powers of a number. However, if the power is big, the task will be tedious. For calculating powers of a number, Python uses ** operator.
- Type following and obtain the results of above expressions.
a. 4 ** 3
b. 4 ** 10
c. 4 ** 29
d. 4 ** 150
e. 4 ** 1000
Activity 6:
Write following math expressions. Solve them by hand using operators’ precedence.
Calculate their answers using Python. Match the results.
Solution:
The table below shows the operator precedence in Python (from Highest to Lowest).

Calculate following expressions:
- 2+3*6
- (2+3)*6
- 48565878 * 578453
- 2 + 2 (note the spaces after +)
- (5 – 1) * ((7 + 1) / (3 – 1))
- 5 +7
- 42 + 5 ** 2
Activity 7:
Combine numbers and text.
Solution:
Type the following code. Run it.

Activity 8:
Take input from the keyboard and use it in your program.
Solution:
In Python and many other programming languages you can get user input. In Python the input() function will ask keyboard input from the user. The input function prompts text if a parameter is given. The function reads input from the keyboard, converts it to a string and removes the newline (Enter).Type and experiment with the script below.

Activity 9:
Let us take an integer from user as input and check whether the given value is even or not.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5

You will get the following output.

Activity 10:
Let us modify the code to take an integer from user as input and check whether the given value is even or odd. If the given value is not even then it means that it will be odd. So here we need to use if-else statement an demonstrated below.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.

You will get the following output

Activity 11:
Calculate the sum of all the values between 0-10 using while loop.
Solution:
Create a new Python file from Python Shell and type the following code.
Run the code by pressing F5.

You will get the following output.

Activity 12:
Accept 5 integer values from user and display their sum. Draw flowchart before coding in python.
Solution:
Create a new Python file from Python Shell and type the following code.
Run the code by pressing F5.

You will get the following output.

Activity 13:
Write a Python code to keep accepting integer values from user until 0 is entered. Display sum of the given values.
Solution:
Create a new Python file from Python Shell and type the following code.
Run the code by pressing F5.

You will get the following output.

Activity 14:
Write a Python code to accept an integer value from user and check that whether the given value is prime number or not.
Solution:
Create a new Python file from Python Shell and type the following code.
Run the code by pressing F5.

You will get the following output(s).

Activity 15:
Write a Python code to accept marks of a student from 1-100 and display the grade according to the following formula.
Grade F if marks are less than 50
Grade E if marks are between 50 to 60
Grade D if marks are between 61 to 70
Grade C if marks are between 71 to 80
Grade B if marks are between 81 to 90
Grade A if marks are between 91 to 100
Activity 16:
Write a program that takes a number from user and calculate the factorial of that number.