Variables and Data types

‫‪Islamic University of Gaza‬‬
‫‪Faculty of Engineering‬‬
‫‪Computer Engineering Dept.‬‬
‫)‪Introduction to Computers Lab (ENGG 1003‬‬
‫ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ‬
‫‪Lab2‬‬
‫‪Variables and Data types‬‬
‫‪Eng. Nour Eldadah‬‬
Variables
Variables are reserved memory locations to store values.
Based on the data type of a variable, the interpreter allocates memory and decides
what can be stored in the reserved memory. Therefore, by assigning different data
types to variables, you can store integers, decimals, or characters in these variables.
Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space. The
declaration happens automatically when you assign a value to a variable.
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously. For
example:
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 objects to
multiple variables. For example:
Identifiers are the names that identify the elements such as classes, methods, and
variables in a program, all identifiers must obey the following rules:
- Identifiers must contain at least one character.
- The first character must be an alphabetic letter (upper or lower case) or the
underscore (_) .
- The remaining characters (if any) may be alphabetic characters (upper or
lower case), the underscore, or a digit .
- No other characters (including spaces) are permitted in identifiers.
- A reserved word (Python Keywords) cannot be used as an identifier.
Standard Data Types:
The data stored in memory can be of many types. Python has various standard data
types that are used to define the operations possible on them and the storage
method for each of them.
Python has 5 standard data types:
- Numbers
- String
- List
- Tuple
- Dictionary
*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:
Python supports four different numerical types:
- int (signed integers)
- long (long integers)
- float (floating point real values)
- complex (complex numbers)
-
Python displays long integers with a lowercase l or an uppercase L .
A complex number consists of an ordered pair of real floating-point numbers
denoted by x + yj, where x is the real part and b is the imaginary part of the
complex number.
Python Arithmetic Operators:
Assume variable a holds 2 and variable b holds 5, then:
+
*
/
%
**
//
Operator
Addition
Subtraction
Multiplication
Division
Modulus
Exponent
Floor Division
Example
a+b=7
a – b = -3
a * b = 10
b/a=2
b%a=1
a**b =32
9//2 = 4 and 9.0//2.0 = 4.0 but
9/2 = 4 and 9.0/2.0 = 4.5
Python Assignment Operators:
Operator
=
+=
add and assign
-=
subtract and assign
*=
multiply and assign
/=
divide and assign
%= modulus and assign
**= exponent and assign
//= floor divison and assign
Example :
Output of Example :
Example
c = a + b assigns value of a + b into c
c += a is equivalent to c = c + a
c -= a is equivalent to c = c - a
c *= a is equivalent to c = c * a
c /= a is equivalent to c = c / a
c %= a is equivalent to c = c % a
c **= a is equivalent to c = c ** a
c //= a is equivalent to c = c // a
Python Strings:
-
Strings in Python are identified as a contiguous set of characters represented
in the quotation marks either pairs of single or double quotes.
Subsets of strings can be taken using the slice operator ([ ] and [:]) with
indexes starting at 0 in the beginning of the string .
The plus (+) sign is the string concatenation operator.
The asterisk (*) is the repetition operator. For example:
Example :
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 .
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 for
Example use of the input function assigns a string to a variable x
x = input()
Examples:
1-
2-
Exercises :
-
Write all the examples in the lab by yourself and put them in your report
.
-
What happens if you attempt to use a variable within a program, and
that variable has not been assigned a value?
-
Assume a = 22 , and b = 4 .. write python program that finds the value
of:
abcde-
a+b
a-b
a*b
a/b
a%b
fg-
-
a**b
a//b
Assume str = 'Nour Eldadah' or your own name , then print :
abcdefgh-
All the string .
The last character in the first name . ( hint : r in Nour )
The first name only . (hint : Nour )
The last name only . (hint : Eldadah)
The name concatenated with " hello" .
The name repeated 3 times .
The length of str .
The type of str .
-
Write a program that let the user enter two numbers , then calculate
their addition and subtraction and print them .
-
Classify each of the following as either a legal or illegal Python
identifier:
abcdefghijklmn-
Salim
If
2x
-4
sum_total
sumTotal
sum-total
sum total
public
$16
_4
___
a27834
wilma’s