11/26/24, 7:42 PM Python_INTERN_001.
ipynb - Colab
keyboard_arrow_down Python Basics
Welcome to Python tutorial. For your benefit we have broken down the entire tutorial into
multiple parts so that you can take it step by step. Please learn Python in the sequence of the
notebooks and topic wise as per the Table Of Contents.
Wish you a Happy Learning!
Table of contents
1. Python Keywords
2. Identifiers
3. Comments in Python
4. Python Indentation
5. Python Statement
keyboard_arrow_down 1. Python Keywords
Keywords in Python are reservered words that cannot be used as ordinary identifiers.
They must be spelled exacty as they are written.
The 33 keywords in Python 3.12 are:
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 1/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
#To extract all keywords in python 3.12 use the below code
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
from platform import python_version
print(python_version())
3.10.12
# Number of keywords are
print("keywords in python are:", len(keyword.kwlist))
keywords in python are: 35
keyboard_arrow_down 2. Identifiers
Identifiers (also referred to as names) is given to entities like class, functions, variables etc. in
Python. It helps differentiating one entity from another.
Rules for Writing Identifiers:
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or
digits (0 to 9) or an underscore (_).
2. An identifier cannot start with a digit. '99somename' is invalid, but 'somename99' is
perfectly fine.
3. Keywords cannot be used as identifiers.
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 2/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
99somename = 100 #Wrong way of defining identifiers
File "<ipython-input-6-9b2f0303c96f>", line 1
99somename = 100 #Wrong way of defining identifiers
^
SyntaxError: invalid decimal literal
Next steps: Fix error
somename99 = 100 # Right way of defining identifiers
print(somename99)
100
breaks == 1 # Cannot use keyword as identifiers
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-773fd1812949> in <cell line: 1>()
----> 1 breaks == 1 # Cannot use keyword as identifiers
NameError: name 'breaks' is not defined
Next steps: Explain error
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
hello@hello == 10 #can't use special symbols as an identifier
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-dca3419a1ce1> in <cell line: 1>()
----> 1 hello@hello == 10 #can't use special symbols as an identifier
NameError: name 'hello' is not defined
Next steps: Explain error
keyboard_arrow_down Few more correct identifiers
hello = 10
print('variable1: ',hello)
hello123 = 50
print('variable2: ',hello123)
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 3/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
hello_1 = "hi"
print('variable3: ',hello_1)
variable1: 10
variable2: 50
variable3: hi
print("welcome to python")
welcome to python
keyboard_arrow_down 3. Comments in Python
A comment may appear at the start of a line or following whitespace or code, but not within a
string literal.
A hash character within a string literal is just a hash character.
Comments are lines that are ignored by compilers and interpreters.
This is one of the best way to convey the idea behind the code and people
reusing your code can easily make sense of your code
keyboard_arrow_down Single line comment
# This is a single line comment. Has no effect on the code below
abc= 10
print(abc)
10
b=10
c=9
d="hi"
print(b)
print(c)
print(d)
10
9
hi
keyboard_arrow_down Multi line comment
# You can write multiple lines of comment by putting a '#' at the beginning
# You can write multiple lines of comment by putting a '#' at the beginning
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 4/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
# You can write multiple lines of comment by putting a '#' at the beginning
keyboard_arrow_down Docstring
Docstring is short for documentation string.
String that occurs as the first statement in a module, function, class,
or method definition. Used to define what a function/class does.
"""
A better option to write multi line comment is by adding all your
text between a set of 3-single or 3-double quotes. Like we have done here
"""
'\nA better option to write multi line comment is by adding all your \ntext between
a set of 3-single or 3-double quotes. Like we have done here\n'
'''
A better option to write multi line comment is by adding all your
text between a set of 3-single or 3-double quotes. Like we have done here
'''
def square(num):
"""
Function to square a number.
Print this docstring using __doc__ attribute of the function
"""
return num * num
print(square(20))
400
print(square.__doc__) # To print the docstring of a function use the __doc__ attribu
Function to square a number.
Print this docstring using __doc__ attribute of the function
keyboard_arrow_down 4. Python Indentation
Unlike C, C++, Java where we use braces { } to define a block of code, in Python we use
indentation.
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 5/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
A code block (body of a function, loop etc.) starts with indentation and ends with the first
unindented line. The amount of indentation is up to you, but it must be consistent throughout
that block.
Generally four whitespaces are used for indentation and is preferred over tabs.
Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the
indentation level of the line, which in turn is used to determine the grouping of statements.
variable1 = 10
variable2 = 20 # Logical continuation disrupted
variable1 = 10
variable2 = 20
File "<ipython-input-19-5ed2d2541249>", line 2
variable2 = 20
^
IndentationError: unexpected indent
Next steps: Explain error
variable1 = 10
variable2 = 20
for i in range (1,4):
print(i) # Logical continuation maintained
print("hello")
print("end")
1
hello
2
hello
3
hello
end
keyboard_arrow_down 5. Python Statement
Instructions that a Python interpreter can execute are called statements. For example, a = 1 is an
assignment statement. if statement, for statement, while statement etc. are other kinds of
statements which will be discussed later.
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 6/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
keyboard_arrow_down Single-line statement
number = 10
num = 12,23,24,25
print(number)
print(num)
10
(12, 23, 24, 25)
keyboard_arrow_down Multi-line statement
(Use a continuation character '')
number = 10 + 20 + \
30 + 40 + \
50
#num1=10
#num2=20
#num3=30
#num4=40
print(number)
150
sum=num1+num2+num3+num4+num5
print(sum)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-27-ea5659dbbb23> in <cell line: 1>()
----> 1 sum=num1+num2+num3+num4+num5
2 print(sum)
NameError: name 'num1' is not defined
Next steps: Explain error
(Alternately use brackets)
number = (10 + 20 +
30 + 40 +
50)
print(number)
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 7/8
11/26/24, 7:42 PM Python_INTERN_001.ipynb - Colab
150
https://colab.research.google.com/drive/1-6i1h3-f-dCUCiBlzhXCR3sNZvUqVGri#scrollTo=uSleNdz0iTnN&printMode=true 8/8