0% found this document useful (0 votes)
56 views4 pages

Python Modules PDF

Uploaded by

saianji964
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)
56 views4 pages

Python Modules PDF

Uploaded by

saianji964
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

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, [Link])
[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 [Link] 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 [Link]
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
[Link]() Returns the arc cosine of a number
[Link]() Returns the arc sine of a number
[Link]() Returns the arc tangent of a number in radians
[Link]() Rounds a number up to the nearest integer
[Link]() Rounds a number down to the nearest integer
[Link]() Returns the factorial of a number
[Link]() Returns the remainder of x/y
[Link]() Returns the greatest common divisor of two integers
[Link]() Rounds a square root number downwards to the nearest integer

[Link]() Returns the natural logarithm of a number, or the logarithm of


number to base
[Link]() Returns the value of x to the power of y
[Link]() Returns the product of all the elements in an iterable
[Link]() Returns the closest value that can make numerator completely
divisible by the denominator
[Link]() Returns the square root of a number
[Link]() Returns the tangent of a number
[Link]() Checks whether a value is Nan (not a number) or not
Math Constants
[Link] Returns PI (3.1415...)
[Link] 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 [Link]() method returns a random float number between 0.0 to 1.0.
The function doesn't need any arguments.
Example: random()
>>> import random
>>> [Link]()
0.645173684807533
Generate Random Integers
The [Link]() method returns a random integer between the specified
integers.
Example: randint()
>>> import random
>>> [Link](1, 100)
95
>>> [Link](1, 100)
49
Generate Random Numbers within Range

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