Uploaded by Dr. P. S. Raja

python-nm-scheme -python-1

advertisement
7/26/23, 6:57 PM
python-nm-scheme - Jupyter Notebook
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
It is one kind of identifier.
Identifier
User defined words
Rules to define and 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
Assigning single value to multiple variables
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/python-nm-scheme.ipynb
1/13
7/26/23, 6:57 PM
>
>=
<
<=
==
!=
python-nm-scheme - Jupyter Notebook
==> Greater than
==> Greater than or equal to
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/python-nm-scheme.ipynb
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)
2
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/python-nm-scheme.ipynb
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))
['Formatter', 'Template', '_ChainMap', '__all__', '__builtins__', '__cache
d__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__s
pec__', '_re', '_sentinel_dict', '_string', 'ascii_letters', 'ascii_lowerc
ase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits',
'printable', 'punctuation', 'whitespace']
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/python-nm-scheme.ipynb
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]:
#keywords or reserved words
import keyword
print(keyword.kwlist)
print(len(keyword.kwlist))
"""
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'co
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import
'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'wit
"""
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'brea
k', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finall
y', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonloc
al', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yiel
d']
35
In [26]:
#Numeric data types
'''
int
float
complex
'''
#string data type - str
#boolean - bool
a = 10
print(type(a)) # returns the data type of a
<class 'int'>
localhost:8888/notebooks/python-nm-scheme.ipynb
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)
8
In [32]:
a=0O123
print(a)
83
In [33]:
a = 0x123
print(a)
291
In [34]:
a = 0b10100
print(a)
20
localhost:8888/notebooks/python-nm-scheme.ipynb
6/13
7/26/23, 6:57 PM
python-nm-scheme - Jupyter Notebook
In [35]:
b = 0O10.5
print(b)
Cell In [35], line 1
b = 0O10.5
^
SyntaxError: invalid syntax
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(data.real)
print(data.imag)
10.0
20.0
In [41]:
data1 = 10j
print(data1.real)
print(data1.imag)
0.0
10.0
localhost:8888/notebooks/python-nm-scheme.ipynb
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)
Murugan looks handsome
In [43]:
string1 = 'Murugan looks "handsome"'
print(string1)
Murugan looks "handsome"
In [44]:
string1 = '''Murugan's friend looks sooo "handsome"'''
print(string1)
Murugan's friend looks sooo "handsome"
In [46]:
aboutRaja = """He is sooo brilliant. He is very good coder.
He is very very talented"""
print(aboutRaja)
He is sooo brilliant. He is very good coder.
He is very very talented
In [47]:
name = "Prabhakaran"
#printing last character of name
print(name[-1])
n
In [48]:
"""
slicing syntax
[<start_index>:<end_index>:<step_index>]
"""
print(name[:])
Prabhakaran
localhost:8888/notebooks/python-nm-scheme.ipynb
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]:
print(name[::-1]) #reverse string
narakahbarP
In [60]:
print(name[:-6:-1])
narak
In [61]:
print(name[-5:]::-1)
Cell In [61], line 1
print(name[-5:]::-1)
^
SyntaxError: invalid syntax
In [63]:
print ((name+"\n") * 10)
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
Prabhakaran
localhost:8888/notebooks/python-nm-scheme.ipynb
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)
['Prabhakaran', 'Male', 120000, True]
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)
['Prabhakaran', 'Male', 120000, True]
Prabhakaran
True
['Prabhakaran', 'Male', 120000]
['Prabhakaran', 120000]
['Prabhakaran', 'Male', 120000, True, 'Aasai', 'Jay']
['Aasai', 'Jay', 'Aasai', 'Jay', 'Aasai', 'Jay', 'Aasai', 'Jay', 'Aasai',
'Jay']
localhost:8888/notebooks/python-nm-scheme.ipynb
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]
TypeError: 'tuple' object doesn't support item deletion
In [76]:
l1 =[1,2,3] #list
t1 =(1,2,3) #tuple
print (l1.__sizeof__(), t1.__sizeof__())
72 48
localhost:8888/notebooks/python-nm-scheme.ipynb
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]:
age = input("Enter your age :")
Enter your age :16
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/python-nm-scheme.ipynb
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]:
print("You are all very active in the class",end="-")
print("You are all very good coder", end="-")
print("Coder never get tired")
You are all very active in the class-You are all very good coder-Coder nev
er get tired
In [ ]:
localhost:8888/notebooks/python-nm-scheme.ipynb
13/13
Download