0% found this document useful (0 votes)
50 views

01.introduction Python JupyterNotebook

This document provides an introduction to Python including examples of basic Python code, data types, operators, functions, errors and exceptions. It demonstrates printing output, comments, variables, basic math operations like addition and subtraction, converting between data types, and the type() function. The document contains examples of strings, integers, floats, Booleans, and their corresponding object types.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

01.introduction Python JupyterNotebook

This document provides an introduction to Python including examples of basic Python code, data types, operators, functions, errors and exceptions. It demonstrates printing output, comments, variables, basic math operations like addition and subtraction, converting between data types, and the type() function. The document contains examples of strings, integers, floats, Booleans, and their corresponding object types.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.06.2022 19:48 01.

introduction_python - Jupyter Notebook

Python Tutorial

Created by Mustafa Germec, PhD

1. Introduction to Python

First code

In [4]:

1 import handcalcs.render

In [2]:

1 # First python output with 'Print' functions


2 print('Hello World!')
3 print('Hi, Python!')

Hello World!
Hi, Python!

Version control

In [10]:

1 # Python version check


2 import sys
3 print(sys.version) # version control
4 print(sys.winver) # [Windows only] version number of the Python DLL
5 print(sys.gettrace) # get the global debug tracing function
6 print(sys.argv) # keeps the parameters used while running the program we wrote in a list.
7

3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]


3.10
<built-in function gettrace>
['c:\\Users\\test\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\ipykernel_laun
cher.py', '--ip=127.0.0.1', '--stdin=9008', '--control=9006', '--hb=9005', '--Session.signature_scheme="hm
ac-sha256"', '--Session.key=b"ca6e4e4e-b431-4942-98fd-61b49a098170"', '--shell=9007', '--transport="t
cp"', '--iopub=9009', '--f=c:\\Users\\test\\AppData\\Roaming\\jupyter\\runtime\\kernel-17668h2JS6UX
2d6li.json']

help() function

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 1/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

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:

displayhook() -- print an object to the screen, and save it in builtins._


excepthook() -- print an exception and its traceback to sys.stderr
exc_info() -- return thread-safe information about the current exception
exit() -- exit the interpreter by raising SystemExit
getdlopenflags() -- returns flags to be used for dlopen() calls
getprofile() -- get the global profiling function
getrefcount() -- return the reference count for an object (plus one :-)
getrecursionlimit() -- return the max recursion depth for the interpreter
getsizeof() -- return the size of an object in bytes
tt () t th l b l d b t i f ti

Comment

In [12]:

1 # This is a comment, and to write a comment, '#' symbol is used.


2 print('Hello World!') # This line prints a string.
3
4 # Print 'Hello'
5 print('Hello')

Hello World!
Hello

Errors

In [13]:

1 # Print string as error message


2 frint('Hello, World!')

---------------------------------------------------------------------------
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!')

NameError: name 'frint' is not defined

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 2/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

In [14]:

1 # Built-in error message


2 print('Hello, World!)

File "C:\Users\test\AppData\Local\Temp/ipykernel_13804/974508531.py", line 2


print('Hello, World!)
^
SyntaxError: unterminated string literal (detected at line 2)

In [15]:

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')

This string is printed

---------------------------------------------------------------------------
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')

NameError: name 'frint' is not defined

Basic data types in Python

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 3/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

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'>

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 4/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

In [25]:

1 # to obtain the information about 'interger' and 'float'


2 print(sys.int_info)
3 print() # to add a space between two outputs, use 'print()' function
4 print(sys.float_info)

sys.int_info(bits_per_digit=30, sizeof_digit=4)

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585


072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-
16, radix=2, rounds=1)

Converting an abject type to another object type

In [35]:

1 # Let's convert the integer number 6 to a string and a float


2
3 number = 6
4
5 print(str(number))
6 print(float(number))
7 print(type(number))
8 print(type(str(number)))
9 print(type(float(number)))
10 str(number)

6
6.0
<class 'int'>
<class 'str'>
<class 'float'>

Out[35]:

'6'

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 5/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

In [37]:

1 # Let's conver the float number 3.14 to a string and an integer


2
3 number = 3.14
4
5 print(str(number))
6 print(int(number))
7 print(type(number))
8 print(type(str(number)))
9 print(type(int(number)))
10 str(number)

3.14
3
<class 'float'>
<class 'str'>
<class 'int'>

Out[37]:

'3.14'

In [42]:

1 # Let's convert the booleans to an integer, a float, and a string


2
3 bool_1 = True
4 bool_2 = False
5
6 print(int(bool_1))
7 print(int(bool_2))
8 print(float(bool_1))
9 print(float(bool_2))
10 print(str(bool_1))
11 print(str(bool_2))
12 print(bool(1))
13 print(bool(0))

1
0
1.0
0.0
True
False
True
False

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 6/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

In [46]:

1 # Let's find the data types of 9/3 and 9//4


2
3 print(9/3)
4 print(9//4)
5 print(type(9/3))
6 print(type(9//4))

3.0
2
<class 'float'>
<class 'int'>

Experesion and variables

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'>

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 7/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

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'>

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebo… 8/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

In [56]:

1 # An example: Let's calculate how many minutes there are in 20 hours?


2
3 one_hour = 60 # 60 minutes
4 hour = 20
5 minutes = one_hour *hour
6 print(minutes)
7 print(type(minutes))
8
9 # An example: Let's calculate how many hours there are in 348 minutes?
10
11 minutes = 348
12 one_hour = 60
13 hours = 348/60
14 print(hours)
15 print(type(hours))

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]:

1 # Store the value 89 into the variabe 'number'


2
3 number = 90
4 print(number)
5 print(type(number))

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

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_noteb… 10/11


1.06.2022 19:48 01. introduction_python - Jupyter Notebook

In [68]:

1 x, y, z = 8, 4, 2 # the values of x, y, and z can be written in one line.


2 print(x, y, z)
3 print(x)
4 print(y)
5 print(z)
6 print(x/y)
7 print(x/z)
8 print(y/z)
9 print(x+y+z)
10 print(x*y*z)
11 print(x-y-z)
12 print(x/y/z)
13 print(x//y//z)
14 print(x%y%z)
15

842
8
4
2
2.0
4.0
2.0
14
64
2
1.0
1
0

localhost:8888/notebooks/Desktop/PROGRAMMING_WEB DEVELOPMENT/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_noteb… 11/11

You might also like