0% found this document useful (0 votes)
5 views7 pages

Lab 6

cf

Uploaded by

aliza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Lab 6

cf

Uploaded by

aliza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

LAB # 6
DECLARAING VARIABLES AND DATA TYPES

OBJECTIVE

To become familiar with the variables and data types.

THEORY

Variable

Variables are nothing but reserved memory locations to store values.


This means thatwhen you create a variable you reserve some space in memory.

Rules for constructing variable names

A variable can have a short name (like x and y) or a more descriptive name
(age, carname,total_volume). Rules for Python variables:

▪ A variable name must start with a letter or the underscore character


▪ A variable name cannot start with a number
▪ A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9,and _ )
▪ Variable names are case-sensitive (age, Age and AGE are three different variables)

CE-119 : Computing Fundamentals 47


Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

Example

x= 5
y= "John"
print(x)
print (y)

Output:
>>> %Run task1.py
5
John

Variables do not need to be declared with any particular type and can even change type
after they have been set.

Example:

x= 4
x= "Sally"
print(x)

Output:
>>> %Run task2.py
Sally

CE-119 : Computing Fundamentals 48


Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

Assign Value to Multiple Variables

Python allows you to assign values to multiple variables in one line

Example:

x, y, z = "Orange", "Banana", "Cherry"


print(x)
print(y)
print(z)

Output:
>>> %Run task3.py
Orange
Banana
Cherry

To combine both text and a variable, Python uses the + character

Example:

x= "awesome"
print("Python is " , x)

Output:
>>> %Run task4.py
Python is awesome

CE-119 : Computing Fundamentals 49


Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

Python Keywords

Keywords are the words whose meaning have already been explained to the Python
compiler. The keywords cannot be used as variable names, function name or any
identifier because if we do so we are trying to assign a new meaning to the keyword,
which is not allowed by the computer. Keywords are also called ‘Reserved words’. Some
keywords are as follows

false class finally is return none continue for try break


true def for from while and del not with as
elif if or except in raise yield

Data Types

Data types specify how we enter data into our programs and what type of data we enter.
Python Data Types are used to define the type of a variable.

Python has five standard data types −


▪ Numbers (int, float)
▪ String
▪ List
▪ Tuple
▪ Dictionary

You can get the data type of any object by using the “type( )” function.

Operators

Operators are special symbols in Python that carry out arithmetic or logical
computation.

CE-119 : Computing Fundamentals 50


Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

Python divides the operators in the following groups:

▪ Arithmetic operators
▪ Assignment operators
▪ Comparison operators
▪ Logical operators
▪ Identity operators
▪ Membership operators
▪ Bitwise operators

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common


mathematicaloperations:

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y

Python Relational Operators

Comparison operators are used to compare two values:

Operator Name Example


== Equal x==y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

CE-119 : Computing Fundamentals 51


Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

Python Logical Operators

Logical operators are used to combine conditional statements:


Operator Name Example
and Return True if both statements are x < 5 and x < 10
true
or Return True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns False if not (x<5 and x< 10)
the
result is true

EXERCISE

A. Point out the errors, if any, in the following Python statements.

x=5:
print(x)

B. Evaluate the operation in each of the following statements and show the
resultant value after each statement is executed.

b=3/2+5*4/3;

CE-119 : Computing Fundamentals 52


Lab # 6: Declaraing Variables and Data Types SSUET/QR/114

Lab Tasks:

1. Write a program to print table till 10 of any assigned number.


Sample Output:
5*1=5
.
.
.
5 * 10 = 50
2. Write a program to print all relational operations using variables.
3. Write a program to calculate the percentage of three subjects.
4. Write a program to calculate the percentage of three subjects by taking marks from
the user.
5. Write a Python program to convert height (in feet and inches) to centimeters.
Convert height of 5 feet 2 inches to centimeters.
▪ First, convert 5 feet to inches: 5 feet × 12 inches/foot = 60 inches
▪ Add up our inches: 60 + 2 = 62 inches
▪ Convert inches to cm: 62 inches × 2.54 cm/inch = 157.48 cm

CE-119 : Computing Fundamentals 53

You might also like