IP Python - Basic-1
IP Python - Basic-1
ABHAY YADAV
PGT (IP)
1 IP@SJC Ranjhi
INTRODUCTION.................................................................................................................................................. 4
TOKEN ................................................................................................................................................................ 5
THE BAREBONES OF PYTHON PROGRAM ARE EXPRESSIONS, STATEMENTS, COMMENTS,FUNCTIONS, BLOCK AND INDENTATION.18
EXPRESSIONS................................................................................................................................................. 18
STATEMENT .................................................................................................................................................. 18
COMMENTS .................................................................................................................................................. 18
FUNCTIONS ................................................................................................................................................... 18
BLOCK AND INDENTATION ................................................................................................................................ 18
VARIABLES ....................................................................................................................................................... 19
2 IP@SJC Ranjhi
DYNAMIC TYPING ........................................................................................................................................... 21
3 IP@SJC Ranjhi
PYTHON FUNDEMENTAL
Introduction
Python 3.0 was released in 2008. Although this version is
supposed to be backward incompatibles, later on many of
its important features have been back ported to be
compatible with version 2.7
4 IP@SJC Ranjhi
Token
Smallest individual unit in a program is known
as token.
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators / Delimiters
Keywords
Keywords are the reserved words in Python.
We cannot use a keyword as variable name, function name
or any other identifier. They are used to define the syntax
and structure of the Python language.
In Python, keywords are case sensitive.
There are 33 keywords in Python 3.7. This number can
vary slightly over the course of time.
5 IP@SJC Ranjhi
Keywords in Python programming language
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
Identifiers
A Python identifier is a name used to identify a variable,
function, class, module or other object.
An identifier is a name given to entities like class,
functions, variables, etc. It helps to differentiate one entity
from another.
* An identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
* Python does not allow special characters
* Identifier must not be a keyword of Python.
* Python is a case sensitive programming language.
Thus, Rollnumber and rollnumber are two different
identifiers in Python.
Some valid identifiers : Mybook, file123, z2td, date_2, _no
Some invalid identifier : 2rno,break,my.book,data-cs
6 IP@SJC Ranjhi
Literals
Literals in Python can be defined as number, text, or
other data that represent values to be stored in variables.
Example of String Literals in Python
name = ‘Johni’ , fname =“johny”
Example of Integer Literals in Python(numeric literal)
age = 22
Example of Float Literals in Python(numeric literal)
height = 6.2
Example of Special Literals in Python
name = None
Escape Characters
Operators
Operators are special symbols in Python that carry out
arithmetic or logical computation. The value that the
operator operates on is called the operand.
Types of Operators
1. Arithmetic Operators.
2. Relational or Compression Operators.
3. Logical Operators.
4. Assignment Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
8 IP@SJC Ranjhi
Arithmetic operators
Arithmetic operators are used to perform mathematical
operations like addition, subtraction, multiplication, etc.
Operator Meaning Example
+ Add two operands or unary plus x + y+ 2
- Subtract right operand from the left or x - y- 2
unary minus
* Multiply two operands x*y
/ Divide left operand by the right one x/y
(always results into float)
% Modulus - remainder of the division of x%y
left operand by the right (remainde
r of x/y)
// Floor division - division that results x // y
into whole number adjusted to the left
in the number line
** Exponent - left operand raised to the x**y (x to
power of right the power
y)
# Output: x + y = 19
print('x + y =',x+y)
9 IP@SJC Ranjhi
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
11 IP@SJC Ranjhi
Logical Operators
Logical Operators are used to perform logical operations
on the given two variables or values.
Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if operand is false (complements the not x
operand)
x = True
y = False
print('x or y is',x or y)
print('not x is',not x)
Output:
x and y is False
x or y is True
not x is False
Assignment operators
Assignment operators are used in Python to assign values
to variables.
is a simple assignment operator that assigns the value
a = 5
equivalent to a = a + 5 .
Operator Example Equivalent to
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x=x&5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Bitwise operators
Bitwise operators act on operands as if they were strings
of binary digits. They operate bit by bit, hence the name.
For example, 2 is 10 in binary and 7 is 111 .
In the table below: Let x = 10 (0000 1010 in binary) and y = 4
(0000 0100 in binary)
Operator Meaning Example
& Bitwise AND x & y = 0 ( 0000 0000 )
| Bitwise OR x | y = 14 (0000 1110 )
13 IP@SJC Ranjhi
~ Bitwise NOT ~x = -11 (1111 0101 )
^ Bitwise XOR x ^ y = 14 (0000 1110 )
>> Bitwise right shift x >> 2 = 2 (0000 0010 )
<< Bitwise left shift x << 2 = 40 (0010 1000 )
Membership operators
in and not in are the membership operators in Python. They
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
Identity operators
is and is not are the identity operators in Python. They are
14 IP@SJC Ranjhi
the same part of the memory. Two variables that are equal
does not imply that they are identical.
Operator Meaning Example
is True if the operands are identical (refer to x is True
the same object)
is not True if the operands are not identical (do not x is not
refer to the same object) True
Example 4: Identity operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
Punctuators / Delimiters
A delimiter is a sequence of one or more characters used
to specify the boundary between separate, independent
regions in plain text or other data streams. An example of
a delimiter is the comma character, which acts as a field
delimiter in a sequence of comma-separated values.
15 IP@SJC Ranjhi
Operators Precedence
() Parentheses
^ Bitwise XOR
| Bitwise OR
16 IP@SJC Ranjhi
= %= /= //= -= Assignment operators
+= *= **=
17 IP@SJC Ranjhi
Barebone of a python program
The barebones of python program are
Expressions, Statements, Comments,Functions,
Block and Indentation.
18 IP@SJC Ranjhi
Variables
Variable is a name given to a memory location. A variable
can be considered as a container which holds value.
Python is a type infer language that means you don't need
to specify the datatype of variable. Python automatically
get variable datatype depending upon the value assigned
to the variable.
Assigning Values to Variable
name = ‘python' # String Data Type
sum = None # a variable without value
a = 23 # Integer
b = 6.2 # Float
sum = a + b
print (sum)
Multiple Assignment: assign a single value to many
variables
a = b = c = 1 # single value to multiple variable
19 IP@SJC Ranjhi
a,b = 1,2 # multiple value to multiple variable
a,b = b,a # value of a and b is swaped
20 IP@SJC Ranjhi
Python Data Types
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different
types can do different things.
Python has the following data types built-in by default, in
these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Dynamic typing
Data type of a variable depend/change upon the value
assigned to a
variable on each next statement.
X = 25 # integer type
X = “python” # x variable data type change to string on
just next line
21 IP@SJC Ranjhi
Now programmer should be aware that not to write like
this:
Y = X / 5 # error !! String cannot be devided
22 IP@SJC Ranjhi
Input and Output :
Python Output Using print() function
We use the print() function to output data to the standard
output device (screen).
An example of its use is given below.
print('This sentence is output to the screen')
Output:
This sentence is output to the screen
23 IP@SJC Ranjhi
print(‘Informatics',‘Practices',sep=' & ',end='.')
Output :-
Informatics Practices
Informatics & Practices
Informatics & Practices.
Python Input
Up until now, our programs were static. The value of
variables was defined or hard coded into the source code.
To allow flexibility, we might want to take the input from
the user. In Python, we have the input() function to allow
this. The syntax for input() is:
input([prompt])
where prompt is the string we wish to display on the
screen. It is optional.
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
24 IP@SJC Ranjhi