03 MRS Python Fundamentals
03 MRS Python Fundamentals
Keywords
Identifiers(Name)
Literals
Operators
Punctuators
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
KEYWORDS
Keywords are the reserved words and have special
meaning for python interpreter. Every keyword is
assigned specific work and it can be used only for that
purpose.
A partial list of keywords in Python is
Boolean Literals
Literal Collections
String Size
„\\‟ 1
„abc‟ 3
„\ab‟ 2
“Meera\‟s Toy” 11
“Vicky‟s” 7
You can check these size using len() function of Python. For example
>>>len(„abc‟) and press enter, it will show the size as 3
>>> isMarried=True
>>> type(isMarried)
<class 'bool'>
>>> salary=None
>>> type(salary)
<class 'NoneType'>
Example 2
>>> percentage=float(input("Enter percentage "))
Enter percentage 100 percent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: „100 percent'
Operator Purpose
+ Unary plus
- Unary minus
~ Bitwise complement
Not Logical negation
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Types of Operators
Binary Operators: are those operators that require two
operand to operate upon. Following are some Binary
operators:
1. Arithmetic Operators
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
** Exponent
// Floor division
Let us see one practical example, To check whether entered number is divisible of 2 (or in
a power of 2 or not) like 2, 4, 8, 16, 32 and so on
To check this, no need of loop, simply find the bitwise & of n and n-1, if the result is 0 it
means it is in power of 2 otherwise not
Identity Operators
Operators Purpose
is Is the Identity same?
is not Is the identity not same?
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Relational Operators
Operators Purpose
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
== Equal to
!= Not equal to
Expressions
Inline Comment
if(C>=100) #checking condition
print(“Value is equals or more than 100”)
else:
print(“Value is less than 100”) Block
Indentation
print(“Welcome to python”)
The above statement call print function
When an expression is evaluated a statement is executed
i.e. some action takes place.
a=100
b = b + 20
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Comments
Comments are additional information written in a
program is not executed by interpreter i.e.
which by Interpreter. Comment contains information
ignored
regarding statements used, program flow, etc.
Comments in Python begins from #
Example
area = length*breadth # calculating area of
Multiline comment rectangle
Example 1 (using #)
# Program name: area of circle
# Date: 20/07/18
#Language : Python
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Comments
Multiline comment (using “ “ “) triple quotes
Example
“““
Program name : swapping of two number
Date : 20/07/18
Logic : by using third variable
”””
def area():
a = 10
b=5
c=a*b
Indentation means extra space before writing any
statement. Generally four space together marks the next
indent level.
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Variables
Variables are named temporary location used to store
values which can be further used in calculations, printing
result etc. Every variable must have its own Identity, type
and value. Variable in python is created by simply
assigning value of desired type to them.
For e.g
Num = 100
Name=“James”
radius = 100
[suppose memory address is 41260]
Now we again assign new value to radius
radius = 500
Now the memory address will be still same only value
will change
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Variables
Now let us take example of Python:
radius = 100 [memory address 3568]
Now you can see that In python, each time you assign new
value to variable it will not use the same memory address
and new memory will be assigned to variable. In python the
location they refer to changes every time their value
change.(This rule is not for all types of variables)
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Lvalues and Rvalues
Lvalue : expression that comes on the Left hand Side
of Assignment.
Rvalue : expression that comes on the Right hand Side
of Assignment
Examples:
y, y = 10, 20
print(x)
Python will show an error „x‟not defined
So to correct the above code:
x=0
print(x) #now it will show no error
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Dynamic Typing
In Python, a variable declared as numeric type can be
further used to store string type or another.
Dynamic typing means a variable pointing to a value of
certain type can be made to point to value/object of
different type.
Lets us understand with example
x = 100 # numeric type
print(x)
x=“KVians” # now x point to string type
print(x)
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Dynamic Typing
string:KVians
x = 100
y=0
y=x/2
print(y)
x='Exam'
y = x / 2 # Error, you cannot divide string
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Determining type of variable
Python provides type() function to check the datatype of
variables.
>>> salary=100
>>> type(salary)
<class 'int'>
>>> salary=2000.50
>>> type(salary)
<class 'float'>
>>> name="raka"
>>> type(name)
<class 'str'>
print(“Name is “, name)
Suggest a solution
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Just a minute…
Name="James"
Salary=20000
Dept="IT"
print("Name is ",Name,end='@')
print(Dept)
print("Salary is ",Salary)
Manash Ranjan Sahoo,PGT(CS), KV No4, BBSR
Just a minute…
www.csipithub.blogspot.com