0% found this document useful (0 votes)
7 views

Lab 2 CS & P Batch 23

Uploaded by

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

Lab 2 CS & P Batch 23

Uploaded by

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

Practical 2

Working in Python Environment: Data Variables and Assignment Operators

Objective:

 To demonstrate types of data variables.


 To familiarize with different assignment operators.
 To implement codes using pseudo codes/flowcharts.
Tools/Software Requirement

 Python 3.0

Theory
a. Demonstrate about Fundamental Data types in Python Programming
Every value in Python has a datatype. Since everything is an object in Python programming, data types
are actually classes and variables are instances (objects) of these classes. There are various data types in
Python. Some of the important types are listed below.

 int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or
a string literal (providing the string represents a whole number)
 float() - constructs a float number from an integer literal, a float literal or a string literal (providing the
string represents a float or an integer)
 str() - constructs a string from a wide variety of data types, including strings, integer literals and float
literal.
Some examples of data types are given as follows:

Fig. 2.1 Examples of data types


Setting the Specific Data Type:
If you want to specify the data type, you can use the following constructor functions:

Fig. 2.2 List of constructor functions for specific data types

Code: Output:

Fig. 2.3(b) Corresponding output for Integers


and Floats
Fig 2.3(a) Sample code for Integers and Floats
Fig. 2.4 Sample code and its corresponding output for string and complex numbers

To verify the type of any object in Python, use the type() function:

Fig. 2.5 Sample code and its output for verification of function type

String Methods:
Python has a set of built-in methods that you can use on strings. Some of them are listed below:
Table 2.1 Different commands that can be used on strings

Method Description

capitalize() Converts the first character to upper case

casefold() Converts string into lower case


center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position
of where it was found
format() Formats specified values in a string
index() Searches the string for a specified value and returns the position
of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a
specified value
rfind() Searches the string for a specified value and returns the last
position of where it was found
rindex() Searches the string for a specified value and returns the last
position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case

Some examples are given as:

Fig. 2.6 Use of String command to capitalize all the letters

b. Demonstrate various operators used in Python.


Operator is a symbol which can perform specified operation on operands.
Types of Operators used in Python:
1. Arithmetic operators.
2. Relational or Comparison operators.
3. Equality operators.
4. Logical operators.
5. Bitwise operators.
6. Shift operators.
7. Assignment operators.
8. Ternary operator. (Or) Conditional operator.
9. Special operators:
a. Identity operator.
b. Membership operator.
10. Operator precedence.

Python Arithmetic Operators:


Arithmetic operators are used with numeric values to perform common mathematical operations:
Table 2.2 List of Arithmetic Operators

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 Assignment Operators:


Assignment operators are used to assign values to variables:
Table 2.3 List of Assignment Operators

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Python Comparison Operators:


Comparison operators are used to compare two values:
Table 2.3 List of Comparison Operators

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

Operator Precedence:
The following list describes operator precedence in Python:

() ==> Parenthesis
** ==> Exponential Operator
~,- ==> Bitwise Complement Operator, Unary Minus Operator
*, /, %, // ==> Multiplication, Division, Modulo, Floor Division
+,- ==> Addition, Subtraction
<<, >> ==> Left and Right Shift
& ==> Bitwise And
^ ==> Bitwise X-OR
| ==> Bitwise OR
<, <=,>,>=,==, != ==> Relational or Comparison Operators
=, +=,-=,*=... ==> Assignment Operators
is, is not ==> Identity Operators
in, not in ==> Membership Operators
Not ==> Logical NOT
and ==> Logical AND
or ==> Logical OR

Lab Task:
1. Execute the following codes to find the output.
a)
b)

c)

d)

Outputs:
2. Get a number from user of any data type and convert to all other types. Also print the data
types to verify.
Codes:

Output:

3. What is the difference between the following two strings? 'n' and '\n'? Write a Python
program containing exactly one print statement that produces the following output:
Difference:

Code:

Output:

4. Consider the following program that attempts to compute the circumference of a circle given the
radius entered by the user. Given a circle’s radius, r, the circle’s circumference, C is given by the formula:
C = 2πr
(a) The program does not produce the intended result. Why?

(b) How can it be repaired so that it works correctly?


Code:

Output
5. Implement the following statements by proposing an algorithm in the form of pseudo code or a
flowchart.
a. Given four numbers (can be user defined), write a Python code to find the Maximum of two
largest numbers.
b. Write a code which print user defined 2 input numbers in sequence and then print again after
swapping their places.
Code:

Output:
Evaluation Criteria:

Un- Developing Satisfactory Good Exemplary Marks


satisfactory (2) (3) (4) (5)
(0-1)
Does not Completed
comply with less than 75%
Completed at Completed
requirements of the Completed
least 75% of between 80-
(does requirements 100% of
the 99% of the
something . requirements
requirements requirements
Organization other than . Code
Not delivered . .
requirements) appears
/ Structure on time or
. Delivered on Code lacks finished and
not in correct
time, and in some minor organized
Information is format (disk,
correct structural properly
presented in a email,
format. continuity.
very mundane Canvas,
way printout etc.)

Poorly Unclear OR
States explicit
Respective written inaccurate
Correct logic results of a
Code and program results due to
but produces program in
Results and result does with faulty minor
no result due accordance
Conclusion not logic and programming
to an error in with the
complement syntax, logic
the code programming
each other producing negligence or
logic
wrong result. syntax error.

Lab Instructor: Dr. Abdullah Waqas/Lab Engr. Wahaj Rafique Total

You might also like