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

Python Modules PDF

Uploaded by

saianji964
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Modules PDF

Uploaded by

saianji964
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python - Basic Syntax

The Python syntax defines a set of rules that are used to create Python statements
while writing a Python Program. The Python Programming Language Syntax has
many similarities to Perl, C, and Java Programming Languages. However, there are
some definite differences between the languages.
First Python Program
Let us execute a Python "Hello, World!" Programs in different modes of
programming.
Python - Interactive Mode Programming
We can invoke a Python interpreter from command line by typing python at the
command prompt as following −
$ python
Python 3.6.8 (default, Sep 10 2021, 09:13:53)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Here >>> denotes a Python Command Prompt where you can type your commands.
Let's type the following text at the Python prompt and press the Enter −
>>> print ("Hello, World!")
If you are running older version of Python, like Python 2.4.x, then you would need
to use print statement without parenthesis as in print "Hello, World!". However in
Python version 3.x, this produces the following result −
Hello, World!
Python - Script Mode Programming
We can invoke the Python interpreter with a script parameter which begins the
execution of the script and continues until the script is finished. When the script is
finished, the interpreter is no longer active.
Let us write a simple Python program in a script which is simple text file. Python
files have extension .py Type the following source code in a test.py file −
print ("Hello, World!")
We assume that you have Python interpreter path set in PATH variable. Now, let's
try to run this program as follows −
$ python test.py
This produces the following result −
Hello, World
Python Math Module:
Python provides the math module to deal with such calculations. Math module
provides functions to deal with both basic operations such as addition(+),
subtraction(-), multiplication(*), division(/) and advance operations like
trigonometric, logarithmic, exponential functions.
Math module provides various the value of various constants like pi, tau. Having
such constants saves the time of writing the value of each constant every time we
want to use it and that too with great precision. Constants provided by the math
module are –
• Euler’s Number
• Pi
• Tau
• Infinity
• Not a Number (NaN)

The following table listing the Math Module methods


Name of the Description
method
math.acos() Returns the arc cosine of a number
math.asin() Returns the arc sine of a number
math.atan() Returns the arc tangent of a number in radians
math.ceil() Rounds a number up to the nearest integer
math.floor() Rounds a number down to the nearest integer
math.factorial() Returns the factorial of a number
math.fmod() Returns the remainder of x/y
math.gcd() Returns the greatest common divisor of two integers
math.isqrt() Rounds a square root number downwards to the nearest integer

math.log() Returns the natural logarithm of a number, or the logarithm of


number to base
math.pow() Returns the value of x to the power of y
math.prod() Returns the product of all the elements in an iterable
math.remainder() Returns the closest value that can make numerator completely
divisible by the denominator
math.sqrt() Returns the square root of a number
math.tan() Returns the tangent of a number
math.isnan() Checks whether a value is Nan (not a number) or not
Math Constants
math.pi Returns PI (3.1415...)
math.tau Returns tau (6.2831...)
Python Random Module:
The random module is a built-in module to generate the pseudo-random
variables. It can be used perform some action randomly such as to get a random
number, selecting a random elements from a list, shuffle elements randomly, etc.
Generate Random Floats
The random.random() method returns a random float number between 0.0 to 1.0.
The function doesn't need any arguments.
Example: random()
>>> import random
>>> random.random()
0.645173684807533
Generate Random Integers
The random.randint() method returns a random integer between the specified
integers.
Example: randint()
>>> import random
>>> random.randint(1, 100)
95
>>> random.randint(1, 100)
49
Generate Random Numbers within Range

The random.randrange() method returns a randomly selected element from the


range created by the start, stop and step arguments. The value of start is 0 by
default. Similarly, the value of step is 1 by default.

Example:
>>> random.randrange(1, 10)
2
>>> random.randrange(1, 10, 2)
5
>>> random.randrange(0, 101, 10)
80
Select Random Elements
The random.choice() method returns a randomly selected element from a non-
empty sequence. An empty sequence as argument raises an Index Error.
Example:
>>> import random
>>> random.choice('computer')
>>> random.choice([12,23,45,67,65,43])
45
>>> random.choice((12,23,45,67,65,43))
67
Shuffle Elements Randomly

The random. Shuffle() method randomly reorders the elements in a list.

Example:
>>> numbers=[12,23,45,67,65,43]
>>> random.shuffle(numbers)
>>> numbers
[23, 12, 43, 65, 67, 45]
>>> random. Shuffle(numbers)
>>> numbers
[23, 43, 65, 45, 12, 67]

List of all the functions in Random Module:


Function name Description
seed() Initialize the random number generator
randrange() Returns a random number within the range
randint() Returns a random integer within the range
choice() Returns a random item from a list, tuple, or string
choices() Returns multiple random elements from the list with
replacement
sample() Returns a particular length list of items chosen from the
sequence
random() Generate random floating numbers
gauss() Return a random floating-point number with Gaussian
distribution

You might also like