Python Modules PDF
Python Modules PDF
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)
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
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]