1.python Introduction
1.python Introduction
Introduction to python
Installing python
Hello World program using python
Data types
Variables
Numbers
Introduction to Python
Python is an open source, interpreted, high-level, general-purpose programming language.
Python's design philosophy emphasizes code readability with its notable use of significant
whitespace.
Python is dynamically typed and garbage-collected language.
Python was conceived in the late 1980s as a successor to the ABC language.
Python was Created by Guido van Rossum and first released in 1991.
Python 2.0, released in 2000,
introduced features like list comprehensions and a garbage collection system with reference counting.
Python 3.0 released in 2008 and current version of python is 3.8.3 (as of June-2020).
The Python 2 language was officially discontinued in 2020
3
Why Python?
Python has many advantages
Easy to learn
Less code
Syntax is easier to read
Open source
Huge amount of additional open-source libraries
Some libraries listed below.
matplotib for plotting charts and graphs
BeautifulSoup for HTML parsing and XML
NumPy for scientific computing
pandas for performing data analysis
SciPy for engineering applications, science, and mathematics
Scikit for machine learning
Django for server-side web development
And many more..
4
Installing Python
For Windows & Mac:
To install python in windows you need to download installable file from https://www.python.org/downloads/
After downloading the installable file you need to execute the file.
For Linux :
For ubuntu 16.10 or newer
sudo apt-get update
sudo apt-get install python3.8
To verify the installation
Windows :
python --version
Linux :
python3 --version (linux might have python2 already installed, you can check python 2 using python --version)
Alternatively we can use anaconda distribution for the python installation
http://anaconda.com/downloads
Anaconda comes with many useful inbuilt libraries.
5
Hello World using Python
To write python programs, we can use any text editors or IDE (Integrated Development
Environment), Initially we are going to use Visual Studio Code.
Create new file in editor, save it as first.py (Extensions for python programs will be .py)
first.py Python line does not end with ;
1 print("Hello World from python")
To run the python file open command prompt and change directory to where your python file is
6
Data types in Python
Name Type Description
Data Types
Integer int Whole number such as 0,1,5, -5 etc..
Float float Numbers with decimal points such as 1.5, 7.9, -8.2 etc..
String str Sequence of character (Ordered) such as “mits”, ‘college’, “રાજકોટ” etc..
Boolean bool Logical values indicating True or False (T and F here are capital in python)
Data Structures
Ordered Sequence of objects, will be represented with square brackets [ ]
List list
Example : [ 18, “mits”, True, 102.3 ]
Ordered immutable sequence of objects, will be represented with round brackets ( )
Tuple tup
Example : ( 18, “mits”, True, 102.3 )
Unordered collection of unique objects, will be represented with the curly brackets { }
Set set
Example : { 18, “mits”, True, 102.3 }
Unordered key : value pair of objects , will be represented with curly brackets { }
Dictionary dict
Example : { “college”: “mits”, “code”: “054” }
7
Variables in Python
A Python variable is a reserved memory location to store values.
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Python uses Dynamic Typing so,
We need not to specify the data types to the variable as it will internally assign the data type to the variable
according to the value assigned.
we can also reassign the different data type to the same variable, variable data type will change to new data
type automatically.
We can check the current data type of the variable with type(variablename) in-built function.
Rules for variable name
Name can not start with digit
Space not allowed
Can not contain special character
Python keywords not allowed
Should be in lower case
8
Example of Python variable
Example :
demo.py
1 x = 10
2 print(x)
3 print(type(x))
Reassign same variable to hold different data type
4
5 y = 123.456
6 print(y)
7
8 x = "mits insitute of engineering and technology"
9 print(x)
10 print(type(x))
Run in terminal
1 python demo.py
Output
1 10
2 int
3 123.456
4 mits institute of engineering and technology
5 str
9
Numbers in python
The following are the supporting number formats:
Integers
Floating point
Complex Numbers
Decimal
Rational
Keywords
10
‘Print’ method
To print any data in output console, use print( ) method
Different formats
Example
11
Runtime Input
To get any data in run-time, use input( ) method
By default, all inputs will be in string format
Example
a=input("Enter a number")
print(a) // string data
b=input()
print(b) // string data
To convert the string data into specific format, use the conversion method
Example
a=int(input("Enter a number"))
print(a) // int data
b=float(input())
print(b) // float data
12
Numbers in python : int
Integers are zero, positive or negative whole numbers without a fractional part and having
unlimited precision
24 bytes of memory – default
Example Example
>>> a=10
>>> a # a=10
>>> a=-10
>>> a # a=-10
15
Numbers in python : int
Test yourself
Example -1 Example -2
a = 10,20 a = 10_20_30
print(a) print(a)
Output Output
Example -5
(10,20) #tuple Output: 102030 print(oct(74))
Output Output
Output: Syntax Error Output: Syntax Error
16
Numbers in python : float
Float data type has fractional part
Positive and Negative real numbers with a fractional part denoted by the decimal symbol (.) or
scientific notation
Example
123.56
3.142
-1.546
0.23
f=2e400
print(f)
inf
17
Numbers in python : float
Scientific Notation is used as a short representation to express floats having many digits
e2 or E2 Example
a = 1e3
print (a) # 1000.0
a = 1e5
print (a) # 100000.0
a = 3.4556789e2
print (a) # 345.56789
Example
a=5.678848
print('{:.2f}'.format(a))
#5.68
18
Numbers in python : float
To convert any type into float, use float( ) function
Example
Formats
19
Numbers in python : Complex Numbers
It is a number with real and imaginary components
Arithmetic Operators can be applied for any complex numbers {+,-, *,**,/}
a = 6 + 4j
a = a+2
print(a) // 8 + 4j
20
Numbers in python : Complex Numbers
Using ‘*’ operator
a=6 + 4j
a=a*2
print(a) // 12 + 8j i.e both real and imaginary will have updation
Using ‘**’ operator
a=6 + 4j
a=a**2
print(a) // 20+48j i.e both real and imaginary will have updation
Calculation:
= 36 + 24j + 24j + (16*-1)
= 36 + 48j – 16
= 20 + 48j
21
Numbers in python : Rational number
It is represented as a rational fraction m/n . i.e has integer value
Ex: 5/6
Fraction – automatically performs the simplification fraction .
9/18 -> 1/2
Always import the module before implementation
Syntax
from fractions import Fraction
a=Fraction(5,6)
print(a) # 5/6
Example:
a=Fraction('1.33') # 133/100 a=Fraction(‘-1.5') # -3/2
22
Numbers in python : Rational number
Arithmetic operators of +, -, *, /, % and ** can be applied
Syntax
from fractions import Fraction
a=Fraction(5,6) + Fraction(3,2) #a=7/3
b=a – Fraction(3,5) #b=26/15
c=b*Fraction(101,202) #c=13/15
d=c/b #d=1/2
e=d%a #e=1/2
f=Fraction(1,2)**Fraction(1,2) #f=0.70710
23