0% found this document useful (0 votes)
4 views9 pages

11-11_python[1]

The document provides an overview of various mathematical and random functions available in Python's math and random modules. It includes examples of using functions such as sqrt, ceil, floor, fmod, and random number generation methods like random(), uniform(), randint(), randrange(), and choice(). Additionally, it demonstrates how to calculate the area of a circle using user input for the radius.
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)
4 views9 pages

11-11_python[1]

The document provides an overview of various mathematical and random functions available in Python's math and random modules. It includes examples of using functions such as sqrt, ceil, floor, fmod, and random number generation methods like random(), uniform(), randint(), randrange(), and choice(). Additionally, it demonstrates how to calculate the area of a circle using user input for the radius.
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/ 9

11-11-2023

Saturday, November 11, 2023 7:03 PM

sqrt:

from math import*


print(sqrt(16))

output:
4.0p

ceil(x): This function will round the value to the integer greater than or equal to x
value.

ceil(10.5)

ceil(4.6)

ceil(4)

from math import*


print(ceil(10.5))

output:
11

output:
D:\>py program1.py
11

floor(x): This function will round the value to the integer less than or equal to x
value.

floor(10.5)

floor(5)

from math import*


print(floor(10.5))

Python 22 batch 7 pm Page 1


output:
D:\>py program1.py
10

fmod(x,y): This function will give remainder as a output.


The return type is a float data type value

from math import*


print(fmod(4,2))

output:
0.0

abs-->absolute
This function will give output without negative symbol.

from math import*


print(fabs(-12))

output:
12.0

Write a program to calculate area of a circle when radius of a circle is given


a=

pi=3.141592653589793

from math import*


r=int(input('enter the radius of a circle:'))
area=pi*pow(r,2)

print("Area of the circle is:",area)

D:\>py program1.py
enter the radius of a circle:4
Area of the circle is: 50.26548245743669

Random Module:

Python 22 batch 7 pm Page 2


To generate random values we will use random module, random module is a inbuilt
module

In random module there are 5 functions

1.random()
2.uniform()
3.randint()
4.randrange()
5.choice()

1.random()

random() is used to generate random float values between 0 and 1(not inclusive)

0<x<1

**It never generates 0 and it never generates 1

from random import*


print(random())

output:
D:\>py program1.py
0.13764545877139434

D:\>py program1.py
0.6645396333772283

D:\>py program1.py
0.317592306214567

D:\>py program1.py
0.06021067330547558

D:\>py program1.py
0.55280180355482

Python 22 batch 7 pm Page 3


D:\>py program1.py
0.29722681521014693

from random import*


for x in range(10):
print(random())

output:
D:\>py program1.py
0.6195619383673585
0.7650319773672067
0.5412240104114934
0.266770039235096
0.0333846858571627
0.6890254563976113
0.8336981460872318
0.2985762567369882
0.9394901124501817
0.45281382735622444

2.uniform()

syntax:
uniform(begin,end)

This function generates random float values between begin and end

begin<x<end , it never generates begin value and end value

from random import*


print(uniform(1,7))

output:
D:\>py program1.py
5.5272825265391345

D:\>py program1.py
6.768569412098124

Python 22 batch 7 pm Page 4


D:\>py program1.py
6.357483120720277

D:\>py program1.py
4.32691156112012

from random import*


for x in range(5):
print(uniform(3,4))

output:
D:\>py program1.py
3.144048624311151
3.664401110815753
3.25814828067614
3.7498779679331617
3.940810141652418

3.randint()

randint(begin,end)

This function generates random integer values both(begin,end) values will be


included

from random import*


print(randint(1,10))

output:
D:\>py program1.py
9

D:\>py program1.py
8

D:\>py program1.py
2

D:\>py program1.py
Python 22 batch 7 pm Page 5
D:\>py program1.py
9

D:\>py program1.py
5

D:\>py program1.py
10

4.randrange()

randrange(begin,end,step)

*begin value is optional , if begin value is not given then default value is 0.
*step value is optional, if not given then default value is 1

It will generates the values between begin and end-1

from random import*


print(randrange(5))

begin value-->0
step value-->1

output:
D:\>py program1.py
1

D:\>py program1.py
1

D:\>py program1.py
4

D:\>py program1.py
2

from random import*


print(randrange(1,5)) [1,2,3,4]

Python 22 batch 7 pm Page 6


from random import*
print(randrange(1,5)) [1,2,3,4]
D:\>py program1.py
2

D:\>py program1.py
4

D:\>py program1.py
4

D:\>py program1.py
2

from random import*


print(randrange(1,11,2))

[1,3,5,7,9]
D:\>py program1.py
1

D:\>py program1.py
1

D:\>py program1.py
1

D:\>py program1.py
7

choice():

choice is applicable for list,tuple,string.

syntax:

choice(list/tuple/string)

from random import*


fruits=['apple','banana','orange','grapes']
Python 22 batch 7 pm Page 7
fruits=['apple','banana','orange','grapes']
print(choice(fruits))

output:
D:\>py program1.py
grapes

D:\>py program1.py
apple

D:\>py program1.py
apple

D:\>py program1.py
banana

from random import*


fruits=('apple','banana','orange','grapes')
print(choice(fruits))

output:
D:\>py program1.py
apple

D:\>py program1.py
grapes

D:\>py program1.py
orange

from random import*


name='apple'
print(choice(name))

output:
D:\>py program1.py
p

D:\>py program1.py
l
Python 22 batch 7 pm Page 8
l

D:\>py program1.py
p

D:\>py program1.py
a

D:\>py program1.py
a

Python 22 batch 7 pm Page 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy