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

Python exp 1 and 2

The document provides an introduction to Python, highlighting its features, applications, and installation instructions. It covers basic programming concepts such as tokens, keywords, identifiers, literals, operators, and data structures like lists and tuples. Additionally, it includes examples of simple commands, arithmetic functions, and trigonometric functions in Python.

Uploaded by

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

Python exp 1 and 2

The document provides an introduction to Python, highlighting its features, applications, and installation instructions. It covers basic programming concepts such as tokens, keywords, identifiers, literals, operators, and data structures like lists and tuples. Additionally, it includes examples of simple commands, arithmetic functions, and trigonometric functions in Python.

Uploaded by

ARIF K F
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NO: 1

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.

Python is used for:


Python has become one of the most preferred platforms where users can do a variety of work
whether it is developing a website, Machine learning, or developing games and software. Most
industries are using Python as it is free software with most of its libraries are easily available
and easy to install and use.

What can Python do:


1. It can be used on the server to create a Web application.
2. It can be used alongside software to create workflows.
3. It can connect to the database system and it reads and modify files.
4. It can be used to handle big data and perform complex mathematics.
5. It can be used for rapid prototyping or for production-ready software development.

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)

c) Boolean literals: Boolean literals in Python are True, False.

d) Special literal: The special literal is None (Absence of a value )

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

2.INSTALLING PACKAGES FOR PYTHON


1. Go to search and type: cmd command prompt is opened
2. Type: python -m pip install --upgrade pip
3. pip install numpy
4. pip install sympy
5. pip install matplotlib

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

SIMPLE COMMANDS IN PYTHON

BASIC OPERATIONS

1. If a=2 and b=3 find

PROBLEM CODE OUTPUT


a+b print(a+b) 5
a-b print(a-b) -1
b-a print(b-a) 1
Ab print(a*b) 6
a/b print(a/b) 0.666
b/a print(b/a) 1.5
ab print(a**b) 8
ba print(b**a) 9
abab print((a*b)**(a*b)) 46656
(a+b)a+b print((a+b)**(a+b)) 3125
(a)1/2 print(a**.5) 1.41421356
(ab)1/2 print((a*b)**.5) 2.44949
(a+b)1/5 print((a+b)**.5) 2.23607
(ab)1/3 print((a*b)**(1/3)) 1.817121
(a+b+3ab)1/2 print((a+b+3*a*b)**.5) 4.795832

2. Power and logarithmic functions

import math
PROBLEM CODE OUTPUT

log1053 print(math.log(53,10)) 1.7242759

log10 1.5 print(math.log(1.5,10)) 0.17609126


loge 1.5 print(math.log(1.5,math.e)) 0.40546511
e5 print(math.e**5) 148.413159
print(math.exp(5))
23 print(math.pow(2,3)) 8.0
(144)1/2 print(math.sqrt(144)) 12.0

1+i  print(abs(complex(1.0,1.0))) 1.41421356

3. Arithmetic functions that Python offers:

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

cosh ( ) print(math.cosh(math.pi)) 11.5919533

sinh −1 (11.548739) print(math.asinh(11.548739)) 3.1415926

cosh (11.548739) print(math.cosh(11.548739)) 51823.1282

tanh −1 (0.996272) print(math.atanh(0.996272)) 3.1415824

  print(math.sinh(math.pi/4)) 0.8686709
sinh  
4

You might also like