01.introduction Python JupyterNotebook
01.introduction Python JupyterNotebook
Python Tutorial
1. Introduction to Python
First code
In [4]:
1 import handcalcs.render
In [2]:
Hello World!
Hi, Python!
Version control
In [10]:
help() function
In [11]:
1 # The Python help function is used to display the documentation of modules, functions, classes, keywords, etc.
2 help(sys) # here the module name is 'sys'
dllhandle -- [Windows only] integer handle of the Python DLL
winver -- [Windows only] version number of the Python DLL
_enablelegacywindowsfsencoding -- [Windows only]
__stdin__ -- the original stdin; don't touch!
__stdout__ -- the original stdout; don't touch!
__stderr__ -- the original stderr; don't touch!
__displayhook__ -- the original displayhook; don't touch!
__excepthook__ -- the original excepthook; don't touch!
Functions:
Comment
In [12]:
Hello World!
Hello
Errors
In [13]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13804/1191913539.py in <module>
1 # Print string as error message
----> 2 frint('Hello, World!')
In [14]:
In [15]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13804/3194197137.py in <module>
1 # Print both string and error to see the running order
2 print('This string is printed')
----> 3 frint('This gives an error message')
4 print('This string will not be printed')
In [27]:
1 # String
2 print("Hello, World!")
3 # Integer
4 print(12)
5 # Float
6 print(3.14)
7 # Boolean
8 print(True)
9 print(False)
10 print(bool(1)) # Output = True
11 print(bool(0)) # Output = False
12
Hello, World!
12
3.14
True
False
True
False
type() function
In [29]:
1 # String
2 print(type('Hello, World!'))
3
4 # Integer
5 print(type(15))
6 print(type(-24))
7 print(type(0))
8 print(type(1))
9
10 # Float
11 print(type(3.14))
12 print(type(0.5))
13 print(type(1.0))
14 print(type(-5.0))
15
16 # Boolean
17 print(type(True))
18 print(type(False))
<class 'str'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'bool'>
<class 'bool'>
In [25]:
sys.int_info(bits_per_digit=30, sizeof_digit=4)
In [35]:
6
6.0
<class 'int'>
<class 'str'>
<class 'float'>
Out[35]:
'6'
In [37]:
3.14
3
<class 'float'>
<class 'str'>
<class 'int'>
Out[37]:
'3.14'
In [42]:
1
0
1.0
0.0
True
False
True
False
In [46]:
3.0
2
<class 'float'>
<class 'int'>
In [47]:
1 # Addition
2
3 x = 56+65+89+45+78.5+98.2
4 print(x)
5 print(type(x))
431.7
<class 'float'>
In [48]:
1 # Substraction
2
3 x = 85-52-21-8
4 print(x)
5 print(type(x))
4
<class 'int'>
In [49]:
1 # Multiplication
2
3 x = 8*74
4 print(x)
5 print(type(x))
592
<class 'int'>
In [50]:
1 # Division
2
3 x = 125/24
4 print(x)
5 print(type(x))
5.208333333333333
<class 'float'>
In [51]:
1 # Floor division
2
3 x = 125//24
4 print(x)
5 print(type(x))
5
<class 'int'>
In [52]:
1 # Modulus
2
3 x = 125%24
4 print(x)
5 print(type(x))
5
<class 'int'>
In [54]:
1 # Exponentiation
2
3 x = 2**3
4 print(x)
5 print(type(x))
8
<class 'int'>
In [56]:
1200
<class 'int'>
5.8
<class 'float'>
In [57]:
1 # Mathematica expression
2 x = 45+3*89
3 y = (45+3)*89
4 print(x)
5 print(y)
6 print(x+y)
7 print(x-y)
8 print(x*y)
9 print(x/y)
10 print(x**y)
11 print(x//y)
12 print(x%y)
312
4272
4584
-3960
1332864
0.07303370786516854
1067641991672876496055543763730817849611894303069314938895568785412634039540022
1668842874389034129806306214264361154798836623794212717734310359113620187307704
8553130787246373784413835009801652141537511130496428252345316433301059252139523
9103385944143088194316106218470432254894248261498724877893090946822825581242099
3242205445735594289393570693328984019619118774730111283010744851323185842999276
1218679164101636444032930435771562516453083564435414559235582600151873226528287
4086778132273334129052616885240052566240386236622942378082773719975939989126678
9683171279214118065400092433700677527805247487272637725301042917923096127461019
9709972018821656789423406359174060212611294727986571959777654952011794250637017
9853580809082166014475884812255990200313907285732712182897968690212853238136253
3527097401887285523369419688233628863002122383440451166119429893245226499915609
9033727713855480854355371150599738557878712977577549271433343813379749929657561
1090329888355805852160926406122231645709135255126700296738346241869701327318850
6363349028686981626711602285071129130073002939818468972496440163596801441600675
Variables
localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 9/11
1.06.2022 19:48 01. introduction_python - Jupyter Notebook
Variables
In [58]:
90
<class 'int'>
In [62]:
1 x = 25
2 y = 87
3 z = 5*x - 2*y
4 print(z)
5
6 t = z/7
7 print(t)
8
9 z = z/14
10 print(z)
-49
-7.0
-3.5
In [68]:
842
8
4
2
2.0
4.0
2.0
14
64
2
1.0
1
0