Lab 1 Python
Lab 1 Python
Objective
Defining variable
In this part, we will discuss some procedure to define variables name and its types. On a PC that
is installed with Python, open spyder and try executing these codes. This self-study method is
one of the fastest ways to master the basic of python programming.
A variable name, can be defined randomly such as x, and is defined as identifier. Each identifier
may consist of letters, digits and underscores (_) but may not begin with a digit. Python is case
sensitive, so identifier defined as number and Number are different identifiers because one
begins with a lowercase letter and the other begins with an uppercase letter.
In [1]: 3g=10
In [2]: 87=10
In [3]: score_4=10
3g and 87 are invalid names as they begin with a digit whereas score_4 is a valid name
Each value in Python has a type that indicates the kind of data the value represents. There are
three basic types of variables
Page 1
Laboratory 1: Introduction to Python 1.2
If the variable contains the integer value Python displays int (short for integer). The number
with a decimal point value is a floating-point number and python displays float. To represent a
sequence of characters in python we use string. String characters are enclosed in single quotes
(') and also in double quotes (").
Python’s built-in function represented as type determines a value’s type. A function performs a
task when you call it by writing its name, followed by parentheses, (). The parentheses contain
the function’s argument—the data that the type function needs to perform its task.
In [1]: x = 4
In [2]: type(x)
In [3]: y = 4.1
In [4]: type(y)
In [5]: z=’name’
In [6]: type(z)
Assignment 1
In a report, document what each code does. Focus on the specific actions and purposes, rather
than the execution results..
For each of the following expressions, write the value of the expression and the type (of the
value of the expression).
width/2
width/2.0
height/3
1+2*5
delimiter * 5
Laboratory 1: Introduction to Python 1.3
Arithmetic Operation
Many programs perform arithmetic calculations. The following table summarizes the arithmetic
operators, which include some symbols
Assignment 2
1. For each of the following expressions, write the solution of the code. If error occur in
code document the reasons for the error.
7 ×2 210 7
4
7
⌊ ⌋ 13 123
4 ⌊− ⌋
4 0
17 mod 5
3 × (4 - 5) 3×4-5
Laboratory 1: Introduction to Python 1.4
4 3
2. The volume of a sphere with radius r is calculated as π r . What is the volume of a
3
sphere with radius 5cm?
Algebraic Operator
You’ll often create conditions using the comparison operators as shown in the table
To avoid using \' and \" inside strings, you can enclose such strings in triple quotes:
Another way to obtaining information from the user is the built in function input. When python
executes this command it will wait for the user to enter a string character until the user hits the
enter key. These characters are then assigned as a string to a variable. Usually some form of
user prompt will be required (i.e. the question posed to the user). This can be accomplished via
a separate print statement or as an argument to the function. Below is the example which asks
the user for their name and then print it back out
In [1]: print(“What is your name?”)
In [2]: x=input()
In [3]: print (“Hello” , x)
Assignment 3
Laboratory 1: Introduction to Python 1.5
Create two program using Ohm’s law. The first version should ask for current and voltage to
determine and print the resistance. The second should ask for voltage and resistance to
determine and print the current. Test the first program using 4 amps and 20 volts. Test the
second program with 12 volts and 5kohms.
Use input function to define current voltage and resistor parameter as per requirement
or
I = float(input(“What is the current in amps”))
Also use triple quoted string to build a simple version of circuit containing voltage source and
resistance. Hint a simple resistor can be created using front and back slashes as follows
print”---/\/\/\---”)
Lab Report
Start each assignment task with commenting mentioning your data in the form
#Programmer’s roll numbers name and date
Each code should be named properly for future use like for ohm law assignment
should be started as
print(“\t\tOhm’s law Calculator\n”)