Python exp 1 and 2
Python exp 1 and 2
BASICS OF PYTHON
1. INTRODUCTION TO PYTHON
Python is a high-level language and it has become one of the most used programming languages
in business, industries, and education because of its free libraries, downloading, and easy to
use. Python is a popular programming language created in 1990-91 by GUIDO VAN ROSSUM
at the Sticht ing Mathematics Centre in the Netherlands. Python is a good language for teaching
programming both at the basic level and at a more advanced level. Python supports multiple
programming like C, C++, Java, and functional programming etc.
Why Python:
1. It works on different platforms (Windows, Unix, Linux, Mac, Raspberry Pi, etc).
2. It has a simple syntax similar to the English language.
3. Python has a syntax that allows developers to write programs with fewer lines than some
other programming languages.
4. Python runs on an interpreter system which means that code can be executed as soon as it
is written. Hence prototyping can be very quick.
5. Python can be treated in a procedural way in an object-oriented way or in a functional way.
Application of Python:
1. GUI (Graphical User Interface) based desktop applications.
2. Image processing and graphic design application.
3. Scientific and computational application.
4. Web frameworks and web applications.
5. Enterprises and business applications.
6. Language development.
TOKENS IN PYTHON
The smallest unit in a program is known as a Token.
Following are the Python tokens:
i) Keywords
ii) Identifiers (Names)
iii) Literals
iv) Operators
v) Punctuations
1. Keywords: Keywords are pre-defined words with special meanings and these are reserved
words that must not be used as normal identifier names.
These keywords consist of False, True, None, and, or, not, as, is, in, if, elif, else, for, while,
assert, del, break, class, continue, def, except, global, finally, from, import, lambda, nonlocal,
pass, return, try, with, yield.
2. Identifiers: Identifiers are the names given to variables, objects, classes, functions, lists,
and so on. Variable names must not be keywords and must not contain blank spaces.
Variable names are made up of (A – Z, a – z, 0 – 9, underscore) and must not start with a
digit. Valid variable names are ABCD, abcd, AbCd, A345, a_bG78, and product. Invalid
variable names are 6ab, av fd, for, and.
3. Literals:
a) String literals: String literals are sequences of characters surrounded by quotes( both
single, both double, or both triple). Single-line literals are like ‘Hello world’, “my
function”, and “ Mera Bharath Mahan”. Multiline strings are like “programs to find the
sum of the first natural numbers’’. Invalid string literals are ‘hello world”, “ Hello
world’
b) Numeric literals: Numeric literals are integers, float, and complex numbers (in python
complex numbers are identified by a+bJ). c)
4. Operators:
a) arithmetic operators ’ +’ (addition), ‘-‘ (substraction), ‘*’ (multiplication), ‘/’ (division),
‘a**b’ or pow(a,b) (for ab ), a//b (integer part of a/b), a%b (remainder obtained by dividing a
by b)
b) Logical operators and relational: and, or, not, , <=, >=, ==
5. Punctuations: ‘, “, #, \, ( ), [ ], { }, :
6. Input statement: input() is used to input string,int(input()) is used to input integer and
float(input()) is used to input floating value.
7. Output statement: Print statement: a=2 b=3 The print statement is a function its general
form is Print (‘this is’, a, ’that is’, b) Where a and b are pre-assigned variable names. If
the above statement is executed the output will be as, this is 2 that is 3, anything written
between two single quotes or between two double quotes is considered a string.
8. Lists: Lists are defined as a= [ 1,2,3,4], indexing start with 0 i.e. a[0] =1,a[1]=2,a[2[=3,
a[3]=4. The elements of the list may be any data type and are mutable.
9. Tuple: Tuples are the group of any data types separated by commas and are enclosed in
parentheses. The elements of a tuple are immutable.
Examples are a=(1, 2, 3), b=( a’, 2, ”C”).
10. Functions: Functions are defined as def name(arguments): return expression for example
def f(x, y): return x+y+2
INSTALLATION OF PYTHON
Python can be downloaded from the website www.python.org the latest version of Python is
3.8.3 or from www.anaconda.com with 64 bits or 32 bits depending on your machine where
we have Spyder, Jupyter lab, Jupyter Notebook, Orange3, Qt Console, Glueviz and RStudio.
Depending on what type of work you want to perform one can launch any one of these. For
Python, only Spyder or Jupyter Notebook is needed.
1.INSTALLING PYTHON
1. Go to => https://www.python.org/downloads
2. Download the file
3. Double click on downloaded file (check add python 3.7.3 to path)s
4. Install Now
5. Open python IDLE in search or in all program
6. Start working on IDLE
3.INSTALLING ANACONDA
1. Go to =>https://www.anaconda.com/distribution
2. Download
3. Windows
4. anaconda 2019.03 for Windows installer
5. python 3.7 version 64 bit
6. save the file
7. install
8. work on jupyter lab, jupyter notebook or Spyder
EXPERIMENT NO: 2
BASIC OPERATIONS
import math
PROBLEM CODE OUTPUT
import math
CODE OUTPUT
print(math.ceil(1.001)) 2
print(math.floor(1.001)) 1
print(math.factorial(5)) 120
print(math.gcd(10,125)) 5
print(math.trunc(1.001)) 1
print(math.trunc(1.999)) 1
4. Trigonometric functions
import math
PROBLEM CODE OUTPUT
sin print(math.sin(math.pi/4)) 0.70710678
4
cos ( )
print(math.cos(math.pi)) -1
tan print(math.tan(math.pi/6)) 0.57735027
6
If a = , find sin . a=math.pi/4
4 4 0.70710678
print(math.sin(a))
Degree of print(math.degrees(a)) 45
4
Radian of print(math.radians(a)) 0.785398
4
sin −1 print(math.asin(a)) 0.903339
4
sin 2 + cos 2 print(math.sin(a)**2+math.cos(a)**2) 1
4 4
Hypotenuse of right angled
triangle having other sides print(math.hypot(12,5)) 13.0
are 12 and 5.
print(math.atan(0.577350))
tan −1
(0.577350) 0.52359857
5. Hyperbolic functions
import math
PROBLEM CODE OUTPUT
sinh ( ) print(math.sinh(math.pi)) 11.5487394
print(math.sinh(math.pi/4)) 0.8686709
sinh
4