Notebook
April 19, 2025
[ ]: # Components of python
- Literals
- Constants
- identifiers
- reserved words / Keywords
- Statements and Expressions
- Block and indentation
- Comments
[ ]: numpy
1 Literals
• literals are the values that you see in python
• int
• float
• str
• complex
• bool
• None
[ ]: a = 10
b = "Hello"
c = 44.777
2 Constants
• the values tha can not be changed mathematically
• python - Use UPPER case variables to represent the constants
[1]: PI = 3.14 # do not change the value as it is a Constant
G = 9.81 # gravity constant do not change it
[2]: PI
[2]: 3.14
1
[ ]: pi = 3.14
[3]: PI = 500
[4]: PI
[4]: 500
[5]: ABC = 55
abc = 98
[6]: abc
[6]: 98
[ ]: Java script
const a = 500;
[ ]: gravity_constant
# identifiers/variable - it is the name given to a literal
[ ]: abc = 88
abc -----> varibale/ Identifier
88 ----> literal
[ ]: cdf = 500
cdf -----> variable
500 -----> literal
[ ]: # rules to declare a Identifier
- allowed char are A-Z , a-z , 0-9 , _
- can not start with a number
- it can not have numbers only
- Special caharacter are not allowed apart from (_)
- It should not have Space in between
- identifiers are case sensitive
- Should not use Reserved words
[7]: abc = 700
[8]: ABC = 900
[9]: abc
[9]: 700
[10]: ABC
2
[10]: 900
[11]: PTDDYython = 99
PTDDYython
[11]: 99
[12]: lmn123 = 55.8888
lmn123
[12]: 55.8888
[13]: _ = 100
[14]: _
[14]: 100
[15]: lmslmslms = 99
[16]: lmslmslms
[16]: 99
[17]: 7086989687 = 90
Cell In[17], line 1
70 = 90
^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
[18]: var70 = 90
[19]: var70
[19]: 90
[20]: abc123 = 80000
[21]: 342abc = 900000
Cell In[21], line 1
342abc = 900000
^
3
SyntaxError: invalid decimal literal
[22]: $% = 543
Cell In[22], line 1
$% = 543
^
SyntaxError: invalid syntax
[25]: @ = 0
Cell In[25], line 1
@ = 0
^
SyntaxError: invalid syntax
[23]: _ = 40
[24]: _
[24]: 40
[26]: my var = 200
Cell In[26], line 1
my var = 200
^
SyntaxError: invalid syntax
[27]: my_var = 200
[28]: LMN = 99
lmn = 77
[29]: LMN
[29]: 99
[30]: A = 100
4
[31]: a = 60
[32]: a
[32]: 60
[ ]: # Reserved words / Keywords
- There are certain words in python that are defined to do a certain task
[ ]:
[33]: print = 90
[34]: print
[34]: 90
[35]: print("Hello")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[35], line 1
----> 1 print("Hello")
TypeError: 'int' object is not callable
[ ]: for
if
[1]: import keyword
[37]: dir(keyword)
[37]: ['__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'iskeyword',
'issoftkeyword',
'kwlist',
'softkwlist']
5
[2]: print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
[3]: None = 100
Cell In[3], line 1
None = 100
^
SyntaxError: cannot assign to None
[4]: print(dir(__builtins__))
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError',
'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError',
'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning',
'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError',
'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit',
'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError',
'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None',
'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError',
'OverflowError', 'PendingDeprecationWarning', 'PermissionError',
'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning',
'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration',
'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning',
'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError',
'__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__',
'__loader__', '__name__', '__package__', '__spec__', 'abs', 'aiter', 'all',
'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes',
'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec',
'execfile', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr',
'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance',
'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max',
'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print',
'property', 'range', 'repr', 'reversed', 'round', 'runfile', 'set', 'setattr',
'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type',
'vars', 'zip']
6
3 Statements and Expressions
[ ]: a = 500 # statement
[ ]: b = 700 + 99 # expression
[ ]: # Block and indentation
[ ]: Block
few lines of code together are called block
[ ]: # indentation - space
space after the condition(:) in between is indentation
[14]: a = 100
b = 500
if a==100:
print("Begining of the block")
print("a value is 100")
print("b value is 500")
print("End of the block")
print("Out of the block")
Begining of the block
a value is 100
b value is 500
End of the block
Out of the block
[16]: a = 100
b = 500
if a==200:
print("Begining of the block")
print("a value is 100")
print("b value is 500")
print("End of the block")
print("Out of the block")
Cell In[16], line 5
print("a value is 100")
^
IndentationError: unexpected indent
[ ]: if(i=0;i<=10;i++)
{codeline1
code2
7
code
}
[ ]: for i in lst:
print()
[ ]: def fun():
code
4 Comments (#)
• Comments are lines of code to write the explaination
[ ]: # Why comments?
- Explain the code
- Makes code more redable
- Prevent the execution of the unnecessary code lines
[18]: a = 100 # a is declared 100
b = 700 # b is declared 700
c = 600 # c is declared 600
# d = 100
E = 900 # E is a Constant
[19]: d
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[19], line 1
----> 1 d
NameError: name 'd' is not defined
[ ]: a = 100
b = 700
c = 600
d = 10
E = 900
[20]: """
This is a
Multiline Comment
We are writing
in multiline
comments
"""
8
a = 10
[21]: a
[21]: 10
5 Data Types in python
• 1 - Fundamental Datatypes / Primitive data types
– int
– float
– bool
– complex
– str
– None
• 2 - Derived Data types / non primitive data types
– list
– tuple
– dict
– set
– frozenset
6 int
• integers are positive or negative numbers without decimal points (whole numbers)
[22]: a = 99
[23]: a
[23]: 99
[24]: b = 0
[25]: b
[25]: 0
[26]: type(a)
[26]: int
[27]: type(b)
[27]: int
[29]: c = -99999
9
[30]: type(c)
[30]: int
7 float
• floating point numbers are positive or negative numbers with decimal points
[31]: var = 99.001
[32]: var
[32]: 99.001
[33]: type(var)
[33]: float
[34]: f = -60000.00000
type(f)
[34]: float
[35]: print(type(f))
<class 'float'>
8 operations on numbers
[ ]: # Arithmetic operations
(+)-----> addition
(-)-----> Sbtraction
(*)-----> multiplication
(/)-----> float division (it gives decimal value)
(//)-----> floor division( it gives int value)
(%)-----> modulus
(**)-----> exponent
[36]: a = 10
b = 20
[37]: # addition
10 + 20
[37]: 30
[38]: a + b
10
[38]: 30
[39]: c = a + b
c
[39]: 30
[40]: # Subtraction
10 - 20
[40]: -10
[41]: a -b
[41]: -10
[42]: b - a
[42]: 10
[44]: # Multiplication (*)
a * b
[44]: 200
[46]: a = 14
b = 3
[ ]: Float division
[47]: 10/5
[47]: 2.0
[ ]: # floor division
[48]: 10//5
[48]: 2
[49]: 14/3
[49]: 4.666666666666667
[50]: # floor value <--------------- 4.666666666666667---------------->ceil value
[ ]: floor value = 4
[ ]: 2.234358
11
[ ]: floor = 2
[ ]: ceil = 3
[51]: import math
[52]: math.ceil(4.666666666666667)
[52]: 5
[53]: round(4.666666666666667)
[53]: 5
[55]: math.ceil(4.666666666666667 , 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[55], line 1
----> 1 math.ceil(4.666666666666667 , 2)
TypeError: math.ceil() takes exactly one argument (2 given)
[54]: round(4.666666666666667 , 2)
[54]: 4.67
[ ]: # % modulus
Remainder
[ ]: 3)14(4 ----> quotient -----> divisoin
12
---------
2 ------> remainder ---> modulus
[56]: 14%3
[56]: 2
[60]: 10%5
[60]: 0
[ ]: # power/ Exponent (**)
[57]: 4**10
12
[57]: 64
[58]: 4*4*4
[58]: 64
[59]: pwd
[59]: 'C:\\Users\\Lenovo\\Desktop\\7 April 2025'
[ ]: type()
[62]: math.ceil(-4.88)
[62]: 5
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
13
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
14
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
15
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
16
[ ]:
[ ]:
[ ]:
[ ]:
[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