Lab 2 Variable and Data Type I
Lab 2 Variable and Data Type I
Faculty of Engineering
Computer Engineering Department
Lab 2
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously. For
example:
Example 1:
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are
assigned to the same memory location.
You can also assign multiple values to multiple variables. For example:
Example 2:
a, b, c = 1, 2, "IUG"
Identifiers are the names that identify the elements such as classes, methods, and
variables in a program, all identifiers must obey the following rules:
*In this lab we will take just about Numbers and String
Python Numbers:
Number data types store numeric values. Number objects are created when you
assign a value to them. For example:
Example 3
var1 = 10
var2 = 1.5
var3 = 2.5-1j
Python supports four different numerical types:
int (signed integers)
long (long integers)
float (floating point real values)
complex (complex numbers)
Operator Example
+ Addition a+b=7
- Subtraction a – b = -3
* Multiplication a * b = 10
/ Division b/a=2
% Modulus b%a=1
** Exponent a**b =32
// Floor Division 9//2 = 4 and 9.0//2.0 = 4.0 but
c += a
print "c += a , Value of c is " , c
c *= a
print "c *= a , Value of c is " , c
c /= a
print "c /= a , Value of c is " , c
c = 3
c %= a
print "c = 3,c %= a , Value of c is " , c
c **= a
print "c **= a , Value of c is " , c
c //= a
print "c //= a , Value of c is " , c
) shows:
Example 5: String_Python.py
str = 'Python Lab '
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2: 5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "Exam" # Prints concatenated string
print "length str ",len(str)
Conversion functions:
int() : convert string containing an integer number to an integer
float() :convert string containing a floating point number to a floating point number
a = "34" a = "3.4"
print "data type a ",type(a) print "data type a ",type(a)
x = int (a) x = float (a)
print "data type x ",type(x) print "data type x ",type(x)
Output:
User Input:
The print function enables a Python program to display textual information to the user.
Programs may use the input function to obtain information from the user.
x = input ()
Examples:
Example 6: usinginput.py
x = input ("Please enter your name: ")
print "Text entered: ", x
print " Type: " , type(x)
Example 7: usinginput.py
x = input ("Please enter an integer value: ")
y = input("Please enter an integer value: ")
Write program that converts a Fahrenheit degree to Celsius using the formula
5
𝐶𝑒𝑙𝑠𝑖𝑢𝑠 = (𝑓𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡 − 32)
9
Celsius=(5.0/9)*(fahrenheit-32)
Output of Example 8:
Exercises:
1) What happens if you attempt to use a variable within a program, and that variable has
not been assigned a value?
1 inch= 2.54 cm
1 foot =30.48 cm
4) Write a Python program that split up a given number of seconds to hours, minutes, and
seconds
For example:
If the user enters 10000 seconds, the program prints 2 hr, 46 min, 40 sec.