Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
112 views

Week 2 Introduction To Python Programming (Lab Manual)

This document provides an introduction and overview of key concepts for students learning Python programming, including variables, data types, arithmetic operators, input/output, and writing basic programs. It includes examples of declaring different variable types, taking user input, performing calculations, and writing short programs to practice the concepts. Activities throughout guide students to apply the concepts by writing code snippets in their editor. The goal is for students to understand Python's basic program structure and be able to define, assign, and manipulate variables to perform calculations and write simple programs.

Uploaded by

Sahir Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Week 2 Introduction To Python Programming (Lab Manual)

This document provides an introduction and overview of key concepts for students learning Python programming, including variables, data types, arithmetic operators, input/output, and writing basic programs. It includes examples of declaring different variable types, taking user input, performing calculations, and writing short programs to practice the concepts. Activities throughout guide students to apply the concepts by writing code snippets in their editor. The goal is for students to understand Python's basic program structure and be able to define, assign, and manipulate variables to perform calculations and write simple programs.

Uploaded by

Sahir Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Python Programming

Lab Manual 2

________________________________________________________________________
Learning Outcomes:
● Students shall be able to define variables and their use in programming.

● Students shall learn the use of arithmetic operators.

● Students shall learn the use of print function.

● Students shall learn the use of strings.

● 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

Activity: Write these lines of code in your


editor (notepad) to graph the practical understanding of the basic program structure of
the c++ language.

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.

Open a python notebook in the jupyter notebook IDE.

Click on the “Untitled” title to change the name of your notebook.


Give your desired name to the notebook.

Write the following line in the highlighted cell for code in the notebook print("Hello World!")

Press Shift + Enter to execute the line of code.

“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.

For example, we can name the variable as “marks”.

Activity: Write your first code of python language on your jupyter notebook by declaring
a variable.

Example (Integer):

“Congratulations, you have successfully learned how to declare a variable”.

Now, we can assign any number to this marks variable.

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.

Now, similarly, we can store total marks in this variable.


total = 1050
“This process of giving value to a variable is known as “assigning value to variable”.
Activity: Write the above code on your computer editor to store the value in the variable.

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.

Example (Getting input from user):

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 (*).
*****
* *
* *
* *
*****

First Try by yourself.

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.

How to get input?


For this purpose, the python language has a method that is
Input() -> input method

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 (Addition of two numbers by taking user input):


Activity: Write the above code on your editor to get the values of variables “num1” and
“num2” variables from the user and perform their addition.
Similarly, we can use these variables to perform various mathematical tasks as well.

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

First, try yourself.

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.

So, we shall use variables to store the obtained marks of Ali.

Let’s name the variable as “matric” for storing matric marks.


Let’s name the variable as “inter” for storing first-year marks.
Let’s name the variable as “ecat” for storing ecat marks.

Let's declare the variables.

matric;
inter;
ecat;

Now, ask Ali to input the values for these variables.

Now, we need to calculate his aggregate.

This is where we shall incorporate the concepts of “arithmetic expressions”.


Now we need to calculate 25% of matric marks, 45% of first-year marks, and 30% of ecat marks
to calculate Ali’s final aggregate.

We shall more variables to calculate the respective percentage of each result.


Let’s declare these variables as well and use the arithmetic expressions to calculate the
percentage.

You are almost there.


We have successfully built logic for your question.

Now, Let’s add the three individual percentages to calculate the final aggregate.

Let’s tell Ali about his calculated aggregate.


“Congratulations, you have finished your first complete c++ program”.

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.

For example, if the number is 3.


Enter the number: 3
The output should be
The multiples are: 3 6 9

Solution:
Task # 2.
Write a python program to print the first three multiples of two given numbers.

For example, if the input is 3 and 5.


Enter the first number: 5
Enter the second number: 3
The output should be
The multiples of the first number are: 5 10 15
The multiples of the second number are: 3 6 9

Solution:

Output:

Task # 3.
Write a python program to print the sum of the first three multiples of two given numbers.

For example, if the input is 3 and 5.


Enter the first number: 3
Enter the second number: 5
The output should be
The multiples of the first number are: 3 6 9
The multiples of the second number are: 5 10 15
The sum of the two multiples is: 48
Solution:

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.

For example, if the input is


Enter the number: 100
The output should be:
The sum of all the numbers is: 5050
Solution:

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:

You might also like