Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes Computer Science

Rating
-
Sold
-
Pages
118
Uploaded on
30-04-2025
Written in
2024/2025

extensive python notes from beginner to advanced

Institution
Course

Content preview

3/25/2021 Python - Jupyter Notebook




Prepared by Asif Bhat

Python Tutorial
In [103]: import sys
import keyword
import operator
from datetime import datetime
import os



Keywords

Keywords are the reserved words in Python and can't be used as an identifier


In [3]: print(keyword.kwlist) # List all Python Keywords

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'cl
ass', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'fr
om', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']


In [4]: len(keyword.kwlist) # Python contains 35 keywords
Out[4]: 35



Identifiers

An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.


In [13]: 1var = 10 # Identifier can't start with a digit

File "<ipython-input-13-37e58aaf2d3b>", line 1
1var = 10 # Identifier can't start with a digit
^
SyntaxError: invalid syntax




In [14]: val2@ = 35 # Identifier can't use special symbols

File "<ipython-input-14-cfbf60736601>", line 1
val2@ = 35 # Identifier can't use special symbols
^
SyntaxError: invalid syntax



localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 1/118

,3/25/2021 Python - Jupyter Notebook


In [15]: import = 125 # Keywords can't be used as identifiers

File "<ipython-input-15-f7061d4fc9ba>", line 1
import = 125 # Keywords can't be used as identifiers
^
SyntaxError: invalid syntax




In [16]: """
Correct way of defining an identifier
(Identifiers can be a combination of letters in lowercase (a to z) or uppercase (
"""

val2 = 10

In [17]: val_ = 99



Comments in Python
Comments can be used to explain the code for more readabilty.


In [18]: # Single line comment
val1 = 10

In [19]: # Multiple
# line
# comment
val1 = 10

In [20]: '''
Multiple
line
comment
'''
val1 = 10

In [21]: """
Multiple
line
comment
"""
val1 = 10


Statements

Instructions that a Python interpreter can execute.




localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 2/118

,3/25/2021 Python - Jupyter Notebook


In [27]: # Single line statement
p1 = 10 + 20
p1

Out[27]: 30


In [28]: # Single line statement
p2 = ['a' , 'b' , 'c' , 'd']
p2
Out[28]: ['a', 'b', 'c', 'd']


In [26]: # Multiple line statement
p1 = 20 + 30 \
+ 40 + 50 +\
+ 70 + 80
p1
Out[26]: 290


In [29]: # Multiple line statement
p2 = ['a' ,
'b' ,
'c' ,
'd'
]
p2
Out[29]: ['a', 'b', 'c', 'd']



Indentation

Indentation refers to the spaces at the beginning of a code line. It is very important as Python uses
indentation to indicate a block of code.If the indentation is not correct we will endup with
IndentationError error.


In [37]: p = 10
if p == 10:
print ('P is equal to 10') # correct indentation
P is equal to 10

In [38]: # if indentation is skipped we will encounter "IndentationError: expected an inde
p = 10
if p == 10:
print ('P is equal to 10')
File "<ipython-input-38-d7879ffaae93>", line 3
print ('P is equal to 10')
^
IndentationError: expected an indented block




localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 3/118

, 3/25/2021 Python - Jupyter Notebook


In [39]: for i in range(0,5):
print(i) # correct indentation
0
1
2
3
4


In [43]: # if indentation is skipped we will encounter "IndentationError: expected an inde
for i in range(0,5):
print(i)
File "<ipython-input-43-4a6de03bf63e>", line 2
print(i)
^
IndentationError: expected an indented block




In [45]: for i in range(0,5): print(i) # correct indentation but less readable

0
1
2
3
4


In [48]: j=20
for i in range(0,5):
print(i) # inside the for loop
print(j) # outside the for loop
0
1
2
3
4
20



Docstrings

1) Docstrings provide a convenient way of associating documentation with functions, classes,
methods or modules.

2) They appear right after the definition of a function, method, class, or module.


In [49]: def square(num):
'''Square Function :- This function will return the square of a number'''
return num**2




localhost:8889/notebooks/Documents/GitHub/Public/Python/Python.ipynb 4/118

Written for

Institution
Course

Document information

Uploaded on
April 30, 2025
Number of pages
118
Written in
2024/2025
Type
Class notes
Professor(s)
Ken
Contains
All classes

Subjects

$99.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
danielgoossens12

Get to know the seller

Seller avatar
danielgoossens12 tutorpro
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
3
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions