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

Lab 1 Python

Uploaded by

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

Lab 1 Python

Uploaded by

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

CS-103: Introduction to Computer Programming Fall 2023

for Data Sciences


Laboratory 1: Introduction to Python
Instructor: Muhammad Salim Butt UET New Campus

Objective

This laboratory session illustrate some of fundamental of Python Programming related to


 Defining variable
 Arithmetic operators.
 Algebraic Operators

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.

Try defining the following variable names

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

Variable Type Python representation Code


Integer Int x=4
floating-point number Float x = 4.1
String Str x =’name’

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.

Try defining the following code

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)

The built-in print function displays its argument(s) as a line of text


In [7]:print('Welcome to Python!')
In [8]:print(“Welcome to Python!”)
In [9]:print('Welcome’, ‘to’, ‘Python!')

Assignment 1
In a report, document what each code does. Focus on the specific actions and purposes, rather
than the execution results..

Assume that we execute the following assignment statements:


width = 17
height = 12.0
delimiter = '.'

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

Python operation Arithmetic operator Algebraic expression Python expression


Addition + f+7 f+7
Subtraction - f-7 f-7
Multiplication * f.7 f*7
Exponentiation ** f7 f**7
f
True division / f/7
7
f
Floor division // ⌊ ⌋ f//7
7
Remainder (modulo) % r mod 7 r%7

Operator Precedence Rules


Python applies the operators in arithmetic expressions according to the following rules of
operator precedence. These are generally the same as those in algebra:
1. Expressions in parentheses evaluate first, so parentheses may force the order of
evaluation to occur in any sequence you desire. Parentheses have the highest level of
precedence. In expressions with nested parentheses, such as (a / (b - c)), the expression
in the innermost parentheses (that is, b - c) evaluates first.
2. Exponentiation operations evaluate next. If an expression contains several
exponentiation operations, Python applies them from right to left.
3. Multiplication, division and modulus operations evaluate next. If an expression contains
several multiplication, true-division, floor-division and modulus operations, Python
applies them from left to right. Multiplication, division and modulus are “on the same
level of precedence.”
4. Addition and subtraction operations evaluate last. If an expression contains several
addition and subtraction operations, Python applies them from left to right. Addition
and subtraction also have the same level of precedence.

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

Algebraic operator Python operator Sample condition Meaning


> > x>y x is greater than y
< < x<y x is less than y
≥ >= x >= y x is greater than or equal to y
≤ <= x <= y x is less than or equal to y
= == x == y x is equal to y
≠ != x != y x is not equal to y

Other coding information


Sequence Description
\n Insert a newline character in a string. When the string is displayed, for each
newline, move the screen cursor to the beginning of the next line.
\t Insert a horizontal tab. When the string is displayed, for each tab, move the
screen cursor to the next tab stop
\\ Insert a backslash character in a string.
\” Insert a double quote character in a string.
\’ Insert a single quote character in a string.
# Add comment in the code

To avoid using \' and \" inside strings, you can enclose such strings in triple quotes:

In [1]: print("""Display "hi" and 'bye' in 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

In this example we shall use Ohm’s law in the form V =I ∗R

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

print(“What is the current in amps?”)


x=input()
print (x)

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”)

You might also like