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

Lab 03 - Escape Sequence, Casting and Math Function

Uploaded by

mb278427
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)
34 views7 pages

Lab 03 - Escape Sequence, Casting and Math Function

Uploaded by

mb278427
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

Programming Fundamentals SSUET/QR/114

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

Implicit Type Conversion in Python


In this, method, Python converts the datatype into another datatype automatically.
Users don’t have to involve in this process.

Department of Applied Computing and Emerging Sciences 1


Programming Fundamentals SSUET/QR/114

Example:
# Python program to demonstrate
# implicit type Casting

# Python automatically converts


# a to int
a=7
print(type(a))

# Python automatically converts


# b to float
b = 3.0
print(type(b))

# Python automatically converts


# c to float as it is a float addition
c=a+b
print(c)
print(type(c))

# Python automatically converts


# d to float as it is a float multiplication
d=a*b
print(d)
print(type(d))

Output:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>

Explicit Type Conversion in Python


In this method, Python needs user involvement to convert the variable data type
into the required data type.
Examples of Type Casting in Python
Mainly type casting can be done with these data type functions:
• Int(): Python Int() function take float or string as an argument and returns
int type object.
• float(): Python float() function take int or string as an argument and return
float type object.
• str(): Python str() function takes float or int as an argument and returns
string type object.

Python Convert Int to Float


Here, we are Converting Int to Float in Python with the float() function.

Department of Applied Computing and Emerging Sciences 2


Programming Fundamentals SSUET/QR/114

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'>

Python Convert Float to Int


Here, we are Converting Float to int datatype in Python with int() function.
Code:
# Python program to demonstrate
# type Casting

# int variable
a = 5.9

# typecast to int
n = int(a)

print(n)
print(type(n))

Output
5
<class 'int'>

Python Convert String to float


Here, we are casting string data type into float data type with float() function.
Code:
# Python program to demonstrate
# type Casting

# string variable
a = "5.9"

# typecast to float
n = float(a)

print(n)

Department of Applied Computing and Emerging Sciences 3


Programming Fundamentals SSUET/QR/114

print(type(n))

Output
5.9
<class 'float'>

What is math module in Python?


The math module is a standard module in Python and is always available. To use
mathematical functions under this module, you have to import the module using
import math.
It gives access to the underlying C library functions. For example,
Python Mathematical Functions
Python has a built-in module that you can use for mathematical tasks., The math
module has a set of methods and constants.
ceil(x) Returns the smallest integer greater than or equal to x.
copysign(x, y) Returns x with the sign of y
fabs(x) Returns the absolute value of x
factorial(x) Returns the factorial of x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns the remainder when x is divided by y
frexp(x) Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable) Returns an accurate floating-point sum of values in the
iterable
isfinite(x) Returns True if x is neither an infinity nor a NaN (Not a
Number)
isinf(x) Returns True if x is a positive or negative infinity
isnan(x) Returns True if x is a NaN
ldexp(x, i) Returns x * (2**i)
modf(x) Returns the fractional and integer parts of x
trunc(x) Returns the truncated integer value of x
exp(x) Return s e**x
expm1(x) Returns e**x - 1
log(x[, b]) Returns the logarithm of x to the base b (defaults to e)
log1p(x) Returns the natural logarithm of 1+x
log2(x) Returns the base-2 logarithm of x
log10(x) Returns the base-10 logarithm of x
pow(x, y) Returns x raised to the power y
sqrt(x) Returns the square root of x
acos(x) Returns the arc cosine of x

Department of Applied Computing and Emerging Sciences 4


Programming Fundamentals SSUET/QR/114

asin(x) Returns the arc sine of x


atan(x) Returns the arc tangent of x
atan2(y, x) Returns atan(y / x)
cos(x) Returns the cosine of x
hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x) Returns the sine of x
tan(x) Returns the tangent of x
degrees(x) Converts angle x from radians to degrees
radians(x) Converts angle x from degrees to radians
acosh(x) Returns the inverse hyperbolic cosine of x
asinh(x) Returns the inverse hyperbolic sine of x
atanh(x) Returns the inverse hyperbolic tangent of x
cosh(x) Returns the hyperbolic cosine of x
sinh(x) Returns the hyperbolic cosine of x
tanh(x) Returns the hyperbolic tangent of x
erf(x) Returns the error function at x
erfc(x) Returns the complementary error function at x
gamma(x) Returns the Gamma function at x
lgamma(x) Returns the natural logarithm of the absolute value of the
Gamma function at x
pi Mathematical constant, the ratio of circumference of a circle
to it's diameter (3.14159...)
e mathematical constant e (2.71828...)
Example:
# Square root calculation # Area Calculation
Code: Code:
import math import math
a=math.sqrt(4) r=int(input("Enter Radius : "))
print(a) print("The Area=",(math.pi*math.pow(r,2)))
Output: Output:
2 Enter Radius : 2
The Area= 12.566370614359172
Example 2
Code: Output:
import math Square root of y is: 2.0

Department of Applied Computing and Emerging Sciences 5


Programming Fundamentals SSUET/QR/114

x = 28 Power of x and y is: 614656.0


y=4 Logarithm of x is:
3.332204510175204
# return the square root of y Logarithm of y is:
print("Square root of y is: " , 1.3862943611198906
math.sqrt(y)) log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
# returns 28 power of 4 i.e. 28*28*28*28 log1p of x is: 3.367295829986474
print("Power of x and y is: " , exp of a is: 1446257064291.475
math.pow(x, y)) expm1 of a is: 1446257064290.475

# return the logarithm of given value


print("Logarithm of x is: " , math.log(x))
print("Logarithm of y is: " , math.log(y))

# return the logarithm of given value


when base is 10
print("log10 of x is: " , math.log10(x))
print("log10 of y is: " , math.log10(y))

# return the log of x + 1


print("log1p of x is: " , math.log1p(x))

# return a power of 2
print("exp of a is: " , math.exp(x))

# return (a power of 2)-1


print("expm1 of a is: " , math.expm1(x))
Code: Output-1:

Department of Applied Computing and Emerging Sciences 6


Programming Fundamentals SSUET/QR/114

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.

Department of Applied Computing and Emerging Sciences 7

You might also like