Lab 03 - Escape Sequence, Casting and Math Function
Lab 03 - Escape Sequence, Casting and Math Function
Lab # 3
Use of Escape Sequence, casting and Math Function
OBJECTIVE:
Explain the USE of Escape Sequences, casting and mathematical functions used
in python and import command
THEORY:
Escape Sequence
In Python strings, the backslash "\" is a special character, also called the
"escape" character. An escape sequence is a sequence of characters that does not
represent itself when used inside a character or string literal, but is translated
into another character or a sequence of characters that may be difficult or
impossible to represent directly.
Escape
Description Example Output
Sequence
\\ Prints Backslash print ("\\") \
\` Prints single-quote print ("\'") '
\" Pirnts double quote print ("\"") "
print
\n ASCII linefeed ( LF ) hello world
("hello\nworld")
ASCII backspace ( BS ) print ("az" +
\b ac
removes previous character "\b" + "c")
ASCII horizontal tab (TAB).
print
\t Prints *hello
("\t*hello")
TAB
Casting
Type Casting is the method to convert the Python variable datatype into a certain
data type in order to perform the required operation by users. In this article, we
will see the various techniques for typecasting. There can be two types of Type
Casting in Python:
• Python Implicit Type Conversion
• Python Explicit Type Conversion
Example:
# Python program to demonstrate
# implicit type Casting
Output:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>
Example:
# Python program to demonstrate
# type Casting
# int variable
a=5
# typecast to float
n = float(a)
print(n)
print(type(n))
Output:
5.0
<class 'float'>
# int variable
a = 5.9
# typecast to int
n = int(a)
print(n)
print(type(n))
Output
5
<class 'int'>
# string variable
a = "5.9"
# typecast to float
n = float(a)
print(n)
print(type(n))
Output
5.9
<class 'float'>
# return a power of 2
print("exp of a is: " , math.exp(x))
import random 1
print(random.randint(0,9)) Output-2:
8
Task:
• Perform the examples shown.
• Use one operator minimum from all the types of operators shown.
• Write a code to add, subtract, multiply and divide number in a same program.
• To solve the quadratic equation and display the two real roots x1 and x2. The
three inputs will be given through command line use math functions where
needs.
• To roll a dice and generate a general number from 1 to 6 and display that
random number use math functions where needs.
• To Find the following Angle Functions using Math functions collected input
from user by Command Line Input (Sin(), Cos(), Tan())
Tasks to Report:
• What datatype input command returns?
• What is casting?
• Define different functions of end= in print command.
• Define int, float and string datatypes.
• Name few commonly used headers file.