Python NM Scheme Python 1
Python NM Scheme Python 1
Introduction to Python
1. It is general purpose, interpreted, object-oriented, high level and multi-pu
rpose programming language
2. Simple and platform independent
3. Beginner Friendly
4. Open source and portable
Variables
Identifier
1. It may contain alphabets (upper & lower case), digits (0-9) and and undersco
re(_)
2. It should not start with digit
3. Should not be Keyword
4. Strickly case sensitive
5. Length may be anything
a = b = c = 100
a,b,c = 10,20,30
Operators
Arithmetic Operators
+ --> Addition
- ==> Substraction
* ==> Multiplication
/ ==> Division
// ==> Floor Division
% ==> Modulus
** ==> Power
Relational
localhost:8888/notebooks/[Link] 1/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
Logical
and
or
not
Bitwise
& AND
| OR
^ XOR
~ NOT
>> Right shift
<< Left shift
Assignment
=
+=, -=, *=, /=, //=, %=, **=
&=, |=, ^=, >>=, <<=
Membership
in
not in
In [2]:
a = 10
b = 20
c = a & b
print (c)
a = 10
b = 20
c = a | b
print (c)
30
localhost:8888/notebooks/[Link] 2/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [4]:
# a = 10
# b = 20
# c = a ^ b
# print (c)
x = 143
key = 1000
x = x ^ key
print(x)
871
In [5]:
x = x ^ key
print(x)
143
In [6]:
x=10
y = ~x
print(y)
-11
In [7]:
x=10
y = x >> 2
print(y)
In [8]:
x=10
y = x << 3
print(y)
80
In [9]:
a = 10
b = 20
a += b # a = a + b
print(a)
30
localhost:8888/notebooks/[Link] 3/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [10]:
a+=1
print(a)
31
In [11]:
char = 'a'
print (char in "aeiouAEIOU")
True
In [13]:
import string
print(dir(string))
In [15]:
print (string.ascii_letters)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
In [18]:
char = 'J'
print (char in string.ascii_letters and char not in "aeiouAEIOU")
True
In [19]:
a = 10
b = 10
#checking whether both objects refer same memory
print (a is b)
True
In [20]:
print (a==b)
True
localhost:8888/notebooks/[Link] 4/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [21]:
print (id(a))
print(id(b))
2973664018960
2973664018960
In [22]:
l1 = [1,2,3]
l2 = [1,2,3]
print(l1 is l2)
False
In [23]:
print(l1==l2)
True
In [25]:
"""
In [26]:
<class 'int'>
localhost:8888/notebooks/[Link] 5/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [27]:
a = "Selvam"
print(type(a))
<class 'str'>
In [28]:
a = 10.5
print(type(a))
<class 'float'>
In [29]:
age = 10
print(age.__sizeof__())
28
In [30]:
single = True
print(type(single))
<class 'bool'>
In [31]:
a = 0o10
print(a)
In [32]:
a=0O123
print(a)
83
In [33]:
a = 0x123
print(a)
291
In [34]:
a = 0b10100
print(a)
20
localhost:8888/notebooks/[Link] 6/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [35]:
b = 0O10.5
print(b)
In [36]:
b = 10.5e5
print(b)
1050000.0
In [37]:
b = 10.5e-5
print(b)
0.000105
In [38]:
salary = 994_31_15_155
print(salary)
9943115155
In [39]:
data = 10+20j
print(type(data))
<class 'complex'>
In [40]:
print([Link])
print([Link])
10.0
20.0
In [41]:
data1 = 10j
print([Link])
print([Link])
0.0
10.0
localhost:8888/notebooks/[Link] 7/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [42]:
"""
string -> sequence characters
it should be enclosed within single or double or triple quotes
character of string can be extracted using its index. The index starts from 0
Python string supports negative indexing that means reverse index. the reverse index sta
substring of string can be extracted using slicing
string can be repeated using repetition operator (*)
"""
string1 = 'Murugan looks handsome'
print(string1)
In [43]:
In [44]:
In [46]:
In [47]:
name = "Prabhakaran"
#printing last character of name
print(name[-1])
In [48]:
"""
slicing syntax
[<start_index>:<end_index>:<step_index>]
"""
print(name[:])
Prabhakaran
localhost:8888/notebooks/[Link] 8/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [50]:
print(name[:6])
Prabha
In [51]:
print(name[-5:])
karan
In [52]:
print(name[1:7])
rabhak
In [53]:
narakahbarP
In [60]:
print(name[:-6:-1])
narak
In [61]:
print(name[-5:]::-1)
In [63]:
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
localhost:8888/notebooks/[Link] 9/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [66]:
"""
list
It is mutal
collection of different elements enclosed with []
It maintain the sequence
It allows duplicated elements
Particular element can be extracted using its index. The index starts from 0.
It supports reverse indexing
sublist can be extracted using slicing
+ can be used to concatenate two list
* can be used to repeat N times
"""
l1 = ["Prabhakaran", "Male", 120000, True]
tinylist = ["Aasai" , "Jay"]
print(l1)
In [67]:
print(l1)
print(l1[0])
print(l1[-1])
print(l1[:3])
print(l1[::2]) #print alternative elements
print(l1+tinylist)
print(tinylist * 5)
localhost:8888/notebooks/[Link] 10/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [75]:
"""
tuple
It is like list
Collection of different elements enclosed with()
difference
1. It is immutable
2. occupy less memory
3. travesal will be faster
"""
t1 = (1,2,3)
print(type(t1))
l1 = [1,2,3]
print(t1, l1)
l1[0] = 100
print(t1,l1)
# t1[0] = 200 #error
del l1[0]
print(l1)
del t1[0] # error
<class 'tuple'>
(1, 2, 3) [1, 2, 3]
(1, 2, 3) [100, 2, 3]
[2, 3]
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In [75], line 19
17 del l1[0]
18 print(l1)
---> 19 del t1[0]
In [76]:
l1 =[1,2,3] #list
t1 =(1,2,3) #tuple
print (l1.__sizeof__(), t1.__sizeof__())
72 48
localhost:8888/notebooks/[Link] 11/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [77]:
"""
input function
input()
syntax:
input(<prompt>)
"""
name = input()
Aasaithambi Jay
In [78]:
print(name)
Aasaithambi Jay
In [79]:
In [80]:
print(age)
16
In [83]:
"""
output function
print()
syntax:
print(*obj, sep=' ', end='\n', file=sys.)
where
*obj --> variable length objects
"""
print("Selvam")
print("Prabhakaran","is very","active in the class", sep="-")
print("Raja",120000,"Software Engineer", sep=",")
Selvam
Prabhakaran-is very-active in the class
Raja,120000,Software Engineer
localhost:8888/notebooks/[Link] 12/13
7/26/23, 6:57 PM python-nm-scheme - Jupyter Notebook
In [84]:
print("Selvam")
print("Prabhakaran","is very","active in the class", sep="\n")
print("Raja",120000,"Software Engineer", sep=",")
Selvam
Prabhakaran
is very
active in the class
Raja,120000,Software Engineer
In [86]:
You are all very active in the class-You are all very good coder-Coder nev
er get tired
In [ ]:
localhost:8888/notebooks/[Link] 13/13