1 - Variable, Data Types and Operators
1 - Variable, Data Types and Operators
• Reference
• https://www.tutorialspoint.com/python/python_variable_types.htm
• https://www.tutorialspoint.com/python/python_basic_operators.htm
Humans have five basic
senses: sight, hearing,smell, taste and touch. Humans have five
basic senses: touch, sight, hearing, smell and taste. The sensing
organs associated with each sense send information to the brain to
help us understand and perceive the world around us.
• If I ask you to add 2+5
What will happen?
Your brain will add these two numbers and
this addition is in response to my instruction.
you will output the result either by speaking
or by writing.
• Identify the organs for sending input to brain
and output the result
• organs through which information goes to brain
are hands,eyes,mouth,ears,nose
• for output you may use your hands or mouth
Block diagram of a Computer
• If I instruct you to carry out some task then your
brain will work accordingly and you will
communicate the result.
• These set of instructions can be called as
program.
• You may understand various languages and give
response according to the language statements.
• Similarly various languages are used in your
computer to give instructions and these set of
instructions are called as program.
• What is Python?
• Python is a cross-platform programming
language, meaning, it runs on multiple platforms
like Windows, Mac OS , Linux.
• It is free and open source.
• Even though most of today’s Linux and Mac have
Python preinstalled in it, the version might be
out-of-date. So, it is always a good idea to install
the most current version. You can download the
latest version of Python and install it.
• For example you can down load IDLE 2.7
Python/IDLE 3.7 Python via the internet.
• Various inter
• Now there are various ways to start Python.
• 1. Immediate mode
• Instruction results are observed after the
execution of each instruction
• Click IDLE (python 3.7 32 bit) icon and you
observe the shell of immediate mode. We can
directly type in Python expressions and press
enter to get the output.
• >>>
• is the Python prompt. It tells us that the
interpreter is ready for our input. Try typing in
1 + 1 and press enter. We get 2 as the output.
• 2. Script mode
• This mode is used to execute Python program
written in a file. Such a file is called a script.
Scripts can be saved to disk for future use.
Python scripts have the extension .py,
meaning that the filename ends with .py.
• For example: helloWorld.py
• To execute this file in script mode we will use
simply run command and execution will take
place after saving the file/script.
• 3. Integrated Development Environment (IDE)
• We can use any text editing software to write a Python
script file. We just need to save it with
the .py extension.
• But using an IDE can make our life a lot easier. IDE is a
piece of software that provides useful features like
code hinting, syntax highlighting and checking, file
explorers etc. to the programmer for application
development.
• Using an IDE can get rid of redundant tasks and
significantly decrease the time required for application
development.
• IDLE 2.7 Python/IDLE 3.7 Python is a graphical user
interface (GUI) that can be installed and is available
from the official website.
• We can also use other commercial or free IDE
according to our preference.
• Python Comments
• Comments are very important while writing a program. It describes what's
going on inside a program so that a person looking at the source code does
not have a hard time figuring it out. You might forget the key details of the
program you just wrote in a month's time. So taking time to explain these
concepts in form of comments is always fruitful.
• In Python, we use the hash (#) symbol to start writing a comment.
• It extends up to the newline character. Comments are for programmers for
better understanding of a program. Python Interpreter ignores comment.
• #This is a comment
• #print out Hello
• print('Hello')
• Multi-line comments
• If we have comments that extend multiple lines, one way of doing it is to use
hash (#) in the beginning of each line. For example:
• #This is a long comment
• #and it extends
• #to multiple lines
• Another way of doing this is to use triple quotes, either ''' or """.
• These triple quotes are generally used for multi-line strings. But they can be
used as multi-line comment as well. Unless they are not docstrings, they do
not generate any extra code.
• """This is also a
• perfect example of
• multi-line comments"""
Python - Variable Types And Use of Operators
• Variables are reserved memory locations
to store values.
• This means that when you create a variable
you reserve some space in memory.
• Based on the data type of a variable, the
interpreter allocates memory and decides
what can be stored in the reserved memory.
• By assigning different data types to
variables, you can store integers, decimals or
characters in these variables.
Python Identifiers
Rules for writing identifiers
• Identifier is the name given to entities like class, functions,
variables etc. in Python. It helps differentiating one entity
from another.
• Example:4
• a,b,c = 1,2,"john"
• print (a)
• print (b)
• print (c)
• Variables and Assignment
• Variables provide a way to associate names with objects. Consider
the code
• Example 5:
• pi = 3
• radius = 11
• area = pi * (radius**2)
• print (area)
• It first binds the names pi and radius to different objects of type int.
• It then binds the name area to a third object of type int.
• Apt choice of variable names plays an important role in enhancing
• readability.
In Example 6 in first line a and pi, in next line b and
diameter and in third c and area are written in
one line
Example:6
• a = 3.14159 ; pi = 3.14159
• b = 11.2 ; diameter = 11.2
• c = a*(b**2) ; area = pi*(diameter**2)
• print (area)
• print (c)
• Python allows multiple assignment. The statement
• x, y = 2, 3
• binds x to 2 and y to 3. All of the expressions on the
right-hand side of the assignment are evaluated
before any bindings are changed.
example.7
• x, y = 2, 3
• x, y = y, x
• print ('x =', x)
• print ('y =', y)
will print
x=3
y=2
Example:8
print(1,2,3,4)
# Output: 1 2 3 4
print(1,2,3,4,sep='*')
# Output: 1*2*3*4
print(1,2,3,4,sep='#')
# Output: 1#2#3#4
• Output formatting
• Sometimes we would like to format our output to make it look
attractive. This can be done by using the str.format() method. This
method is visible to any string object.
• >>> x = 5; y = 10
• >>> print('The value of x is {} and y is {}'.format(x,y))
x = 12.3456789
• Python Numbers
• Number data types store numeric values. Number objects
are created when you assign a value to them. For example
• var1 = 1
• var2 = 10
• You can delete a single object or multiple objects by
using the del statement. For example −
del var
del var_a, var_b
• Python supports four different numerical types –
i- int (signed integers)
ii- long (long integers, they can also be
represented
in octal and hexadecimal)
iii- float (floating point real values)
iv- complex (complex numbers)
• Integers and floating points are separated by
the presence or absence of a decimal point. 5
is integer whereas 5.0 is a floating point
number.
• Complex numbers are written in the form, x +
yj, where x is the real part and y is the
imaginary part.
• We can use the type() function to know which
class a variable or a value belongs to and is
instance() function to check if it belongs to a
particular class.
• Example 10
• a=5
• # Output: <class 'int'>
• print(type(a))
• # Output: <class 'float'>
• print(type(5.0))
• # Output: (8+3j)
• c = 5 + 3j
• print(c + 3)
• # Output: True
• print(isinstance(c, complex))
• # Output: 107
• print(0b1101011)
• # Output: 253 (251 + 2)
• print(0xFB + 0b10)
• # Output: 13
• print(0o15)
a=10
b=20
c=a<b
d=a!=b
f=a>b
print (c and d)
print (c or d)
print (c and f)
Output is:
True
True
False
Python Bitwise Operators
• Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b =
13; Now in binary format they will be as follows −
• a = 0011 1100
• b = 0000 1101
• There are following Bitwise operators supported by Python language
Operator Description & Example
• & Binary AND Operator copies a bit to the result if it exists in both operands
Example : (a & b) (means 0000 1100)
• | Binary OR It copies a bit if it exists in either operand.
• Exmple: (a | b) = 61 (means 0011 1101)
• ^ Binary XOR It copies the bit if it is set in one operand but not both.
Example: (a ^ b) = 49 (means 0011 0001)
• ~ Binary Ones Complement It is unary and has the effect of 'flipping' bits.
Example:(~a ) = -61 (means 1100 0011 in 2's complement form due to a
signed binary number.
• << Binary Left Shift The left operands value is moved left by the number of bits specified
by the right operand. Example : a << 2 = 240 (means 1111 0000)
• >> Binary Right Shift The left operands value is moved right by the number of bits specified
by the right operand. Example: a >> 2 = 15 (means 0000 1111)
Example 14
a = 60
b = 13
print (a&b)
print (a|b)
print (a^b)
print (~a)
Output is :
12
61
49
-61
Example 15
a = 0b1101
b = 0b1001
print (bin(a&b))
print (bin(a|b))
print (bin(a^b))
print (bin(~a))
Output is:
0b1001
0b1101
0b100
-0b1110
Python Comparison Operators
These operators compare the values on either sides of them and decide the
relation among them. They are also called Relational operators.