Basics of Python
Basics of Python
https://www.python.org/
History
• Python was developed by Guido Van Rossum in the year 1990 at Stichting
Mathematisch Centrum in the Netherlands as a successor of a language
called ABC.
• Name “Python” picked from TV Show Monty Python’s Flying Circus.
https://docs.python.org/3/license.html
Version
• Python 0.9.0 - February, 1991
• Python 1.0 - January 1994
• Python 2.0 - October, 2000
• Python 3.0 - December, 2008
• Python 3.1 - June, 2009
• Python 3.2 - February, 2011
• Python 3.3 - September, 2012
• Python 3.4 - March, 2014
• Python 3.5 - September, 2015
• Python 3.6 - December, 2016
• Python 3.7 - June, 2018
https://www.python.org/doc/versions/
Byte Code – Byte Code represents the fixed set of instruction created by
Python developers representing all type of operations like arithmetic
operations, comparison operation, memory related operation etc.
The size of each byte code instruction is 1 byte or 8 bits.
We can find byte code instruction in the .pyc file.
Python Compiler – A Python Compiler converts the program source code into byte
code.
Type of Python Compilers :-
• CPython
• Jpython/ Jython
• PyPy
• RubyPython
• IronPython
• StacklessPython
• Pythonxy
• AnacondaPython
• Write Source Code / Program
• Compile the Program using Python Compiler
• Compiler Converts the Python Program into byte Code
• Computer/Machine Can not understand Byte Code so we convert it into Machine Code using
PVM
• PVM uses an interpreter which understands the byte code and convert it into machine code
• Machine Code instructions are then executed by the processor and results are displayed
Run Program
using PVM
Source Code/ Binary Code /
Byte Code Computer
Program Compile using Machine Code
Python Compiler
Output
show.py show.pyc
Variable
In C, Java or some other programming languages, a variable is an identifier or
a name, connected to memory location.
a = 30
10 b = 10 c = 20
a b c
10
30 10 20
y=a
y
30
12678
Variable
In Python, a variable is considered as tag that is tied to some value. Python
considers value as objects.
a = 10 b = 10 a = 20
a b a
10 10 20
y=a
y
20
12678
Variable
In Python, a variable is considered as tag that is tied to some value. Python
considers value as objects.
a = 10 a = 20
a a
10 20
12114 12115
• No Reserved keyword
Examples
Do Don’t
•A •and
•a •15name
•name •$city
•name15 •Full$Name
•_city •Full Name
•Full_name
•FullName
Operators
An operator is a symbol that performs an operation.
•Arithmetic Operators
•Relational Operators / Comparison Operators
•Logical Operators
•Assignment Operators
•Bitwise Operators
•Membership Operators
•Identity Operators
Arithmetic Operators
Arithmetic Operators are used to perform basic arithmetic operations like
addition, subtraction, division etc.
Arithmetic Operators
Arithmetic Operators are used to perform basic arithmetic operations like
addition, subtraction, division etc.
Logical Operators
Logical operators are used to connect more relational operations to form a
complex expression called logical expression. A value obtained by evaluating
a logical expression is always logical, i.e. either True or False.
a = 10 0000 1010
b = 15 0000 1111
Bitwise XOR ^
Operand 1 Operand 2 Result (operand1 ^
operand2)
True 1 True 1 False 0
True 1 False 0 True 1
False 0 True 1 True 1
False 0 False 0 False 0
a = 10 0000 1010
b = 15 0000 1111
Bitwise NOT ~
Operand Result (~ operand)
True 1 False 0
False 0 True 1
a = 10 0000 1010
Bitwise Left Shift <<
a = 10
0 0 0 0 1 0 1 0
0 0 1 0 1 0 0 0
a << 2
Bitwise Right Shift >>
a = 10
0 0 0 0 1 0 1 0
0 0 0 0 0 0 1 0
a >> 2
Membership Operators
The membership operators are useful to test for membership in a sequence
such as string, lists, tuples and dictionaries.
There are two type of Membership operator:-
•in
•not in
in
This operators is used to find an element in the specified sequence.
It returns True if element is found in the specified sequence else it returns False.
Ex:-
st1 = “Welcome to geekyshows”
“to” in st1
True
st2 = “Welcome top geekyshows”
“to” in st2
st3 = “Welcome
Trueto geekyshows”
“subs” in st3
False
not in
This operators works in reverse manner for in operator.
It returns True if element is not found in the specified sequence and if element is
found, then it returns False.
Ex:-
st1 = “Welcome to geekyshows”
“subs” not in st1
True
st2 = “Welcome to geekyshows”
“to” not in st2
False
Operator Precedence and Associativity
The computer scans an expression which contains the operators from left to
right and performs only one operation at a time. The expression will be
scanned many times to produce the result. The order in which various
operations are performed is known as hierarchy of operations or operator
precedence. Some of the operators of the same level of precedence are
evaluated from left to right or right to left. This is referred to associativity.
Ord Operator Meaning
er
1 () Parentheses
2 ** Exponentiation
3 +, -, ~ Unary Plus, Unary Minus, Bitwise
Not
4 *, /, //, % Multiplication, Division, Floor
Division, Modulus
5 +, - Addition, Subtraction
6 <<, >> Bitwise Left Shift, Bitwise Right
Shift
7 & Bitwise AND
8 ^ Bitwise XOR
9 >, >=, <, <=, ==, != Relational Operators
10 =, %=, /=, //=, -=, Assignment Operators
+=, *=, **=
11 is, is not Identity Operators
12 in, not in Membership Operators
13 not Logical NOT
14 or Logical OR
• Parentheses value = (1+1)*2**4//3+4-1
2*2**4//3+4-1
• Exponentiation
2*16//3+4-1
• Multiplication, Division, Modulus and Floor 32//3+4-1
Division 10+4-1
• Addition and Subtraction 14-1
• Assignment 13
Input and Output
Input - The data given to the computer is called input.
Output – The results returned by the computer are called output.
Processing
Input Input depends Output
on Logic
Keyboard
Mouse
Database
Output Statements
print ( ) Function - The print() function is used to print the specified message to the output
screen/device. The message can be a string, or any other object.
sep - Separate the objects by given character. Character can be any string. Default is ‘ ’ or can
write none.
end – It indicates ending character for the line. Default is ‘\n’ or can write none.
file - An object with a write method. Default is sys.stdout or can write none.
flush - A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False
Output Statements
print ( ) – This function is used to display a blank line.
print(“string”) - When a string is passed to the function, the string is displayed as it is.
Ex:-
print(“Welcome to Geeky Shows”)
print(‘Welcome to Geeky Shows’)
print(“Like”, “Share”, “Subscribe”)
print(10)
print(“Welcome”)
print(“to”)
print(“Geeky Shows”)
Output Statements
print(object) - We can pass objects like list, tuples and dictionaries to display the elements of
those objects.
Ex:-
data = [10, 20, -50, 21.3, ‘Geekyshows’]
print(data) List
Output Statements
print(“string” sep=‘’) – It separates string with given sep character. Character can be any string.
Default is ‘ ’ or can write none.
Ex:-
print(“Like”, “Share”, “Subscribe”, sep=‘’)
print(“Like”, “Share”, “Subscribe”, sep=‘***’)
Output Statements
print(“string” end=‘’) – When ending character is passed. It prints given character at the end.
Default is ‘\n’ or can write none.
Ex:-
print(“Welcome”, end=‘\n’)
print(“Welcome”, end=‘’)
print(“to”, end=‘’)
print(“GeekyShows”)
print(“Welcome”, end=‘\t’)
print(“to”, end=‘\t’)
print(“GeekyShows”)
Output Statements
print(variable list) – This is used to display the value of a variable or a list of variable.
Ex:-
a = 10
print(a)
Output: 10
x = 20
y = 30
print(x, y) List of variable’s value in the output screen,
Output: 20 30 are separated by a space by default. we can
change this by sep
print(x, y, sep=‘,’)
Output: 20, 30
Output Statements
print(“String”, variable list) – This is used to display the string along with variable.
Ex:-
m = 40
print(“Value: ”, m)
Output: Value: 40
name = “Rahul”
age = 62
print(“My Name is ”, name, “and My age is”, age)
Output: My Name is Rahul and My age is 62
Input Statements
input( ) – This function is used to accept input from keyboard.
This function will stop the program flow until the user gives an input and end the input with the
return key.
Whatever user gives as input, input function convert it into a string. If user enters an integer value
still input() function convert it into a string.
So if you need an integer you have to use type conversion.
Syntax:- input([prompt])
prompt is a string or message, representing a default message before input. It is optional
Ex:-
name = input( )
name = input(“Your Name: ”)
mobile = input(“Enter Your Mobile Number: ”)
Input Statements
Whatever user gives as input, input function convert it into a string. If user enters an integer value
still input() function convert it into a string.
So if you need an integer you have to use type conversion.
Ex:-
mobile = input(“Enter Your Mobile Number: ”)
mb = int(mobile)