Week 2 Introduction To Python Programming (Lab Manual)
Week 2 Introduction To Python Programming (Lab Manual)
Lab Manual 2
________________________________________________________________________
Learning Outcomes:
● Students shall be able to define variables and their use in programming.
● Students shall learn how to get the input from the user.
Introduction
In this class, we will learn about “variables”, “assignment statements”, “arithmetic
operators”, “print function”, “introduction to strings” and “getting input from user”
concepts in python, these concepts are used to write different programs.
Program structure
Just like any other language, the python language also has a particular structure that requires
that instructions must be given according to the defined structure.
Example:
The jupyter notebook rules require that all the instructions must be given inside the highlighted
section, the code file is saved automatically.
Highlighted
area for code
Press New
Button
Select this
option to make a
python notebook
in jupyter
Compiler
We will use the jupyter notebook integrated development environment (IDE) to compile and
execute our programs.
To execute the program, perform the following steps.
Write the following line in the highlighted cell for code in the notebook print("Hello World!")
“Congratulations, You have successfully created, compiled, and executed your first
program”
Variable and Types
In programming, there are “nicknames” that are known as “variables” that are used to store
different kinds of values.
For example,
Ali had obtained 850 marks in matric. We want to store this number in computer memory. But,
the problem is we cannot possibly remember the address of all the things that we stored in the
memory. That’s why we use nicknames, “variables”.
So, we can use a variable to store the obtained marks of Ali in the storage of the computer.
Activity: Write your first code of python language on your jupyter notebook by declaring
a variable.
Example (Integer):
Activity: Write The above code on your computer editor to check the value stored in the
variable.
Example (Integer):
Similarly, we can also declare a variable for storing total marks in this way.
Similarly, let's also check the value stored in the percentage variable.
Example (Float):
Activity: Write The above code on your computer editor to check the value stored in the
variable.
Example (Float):
declare a variable named as “number” to store a value of 400.6.
number = 400.6
Activity: Write the above code on your computer editor to store the value in the variable.
Example (Character):
Declare a variable named letter to store “C”.
Activity: Write the above code in your computer to declare a char type variable in your
program.
Example (String):
name = “John”
Example (String):
Declare a variable named “studentname”.
studentname = “Qaiser”
Activity: Write the above code on your computer editor to declare a string-type variable
in your program.
Ask the user to enter his obtained matric marks through the console.
Activity: Write the above code on your computer to print the above-mentioned line on
your computer screen.
Write a python program that prints a box using stars (*).
*****
* *
* *
* *
*****
Activity: Try to solve the above question by using the knowledge you have learned to
this point.
Don’t worry.
The solution is attached below.
Now, Let’s use it to get your first variable input from the user.
Activity: Write the above code on your editor to get the value of the “name” variable from
the user.
Example:
Consider the following question.
Write a program to calculate current (I) in a wire. The charge (Q) flowing through it in a time (t)
of 5 seconds is 5 Coulombs. Print the current (I) on the screen.
Hint: I = Q / t
Don’t worry.
The solution is attached below. For Now, just try for yourself.
Activity: Write the above code on your editor to get the desired answer.
Scenario
Assume that Ali is a student who wants to calculate his aggregate for taking admission in UET.
we shall use a computer program that would take his obtained marks and after processing, it
would tell Ali his aggregate.
Firstly, we shall need to ask Ali about his marks and store those values somewhere so we can
calculate the final aggregate at the end.
In programming, there are “nicknames” that are known as “variables” that are used to store
such values.
matric;
inter;
ecat;
Now, Let’s add the three individual percentages to calculate the final aggregate.
Now, we shall use all these concepts to perform the tasks that are listed below.
Task # 1.
Write a python program to print the first three multiples of the given number.
Solution:
Task # 2.
Write a python program to print the first three multiples of two given numbers.
Solution:
Output:
Task # 3.
Write a python program to print the sum of the first three multiples of two given numbers.
Task # 4.
The sequence of numbers (1, 2, 3, … , 100) is arithmetic and when we are looking for the sum
of a sequence, we call it a series. Thanks to Gauss, there is a special formula we can use to
find the sum of a series:
Write a program that takes input from the user and prints the sum of consecutive numbers to the
input value.
2Program #5.
Take two numbers as input and find the sum between these two numbers.
Input n1= 2
Input n2=8
Processing answer=2+3+4+5+6+7+8 = 35
Hint:
Formula: (n / 2)(first number + last number) = sum, where n is the number of integers between
two numbers and n= last number - (first number-1)
Solution: