Lab 2 CS & P Batch 23
Lab 2 CS & P Batch 23
Objective:
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:
Code: Output:
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
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?
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:
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.