Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Starterkit 2022-python-and-bash-tutorial
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Richard Morgan Williams
Starterkit 2022-python-and-bash-tutorial
Commits
7658b095
Commit
7658b095
authored
2 years ago
by
Richard
Browse files
Options
Downloads
Patches
Plain Diff
remove old notebook
parent
ebad1a4e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/Starterkit notebook - part 1.ipynb
+0
-806
0 additions, 806 deletions
notebooks/Starterkit notebook - part 1.ipynb
with
0 additions
and
806 deletions
notebooks/Starterkit notebook - part 1.ipynb
deleted
100644 → 0
+
0
−
806
View file @
ebad1a4e
{
"cells": [
{
"cell_type": "markdown",
"id": "ffded810",
"metadata": {},
"source": [
"# LHCb Starterkit - Introduction to python - part 1\n",
"- modules\n",
"- running other python scripts"
]
},
{
"cell_type": "markdown",
"id": "10212d4a",
"metadata": {},
"source": [
"## basic obejcts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8082fd8",
"metadata": {},
"outputs": [],
"source": [
"string = \"Hello World\"\n",
"\n",
"a_list = [1,5.04,string]\n",
"\n",
"dictionary = {\"element 1\":1, \"element 2\": a_list, \"element 3\":string}"
]
},
{
"cell_type": "markdown",
"id": "aef29764",
"metadata": {},
"source": [
"## basic functions"
]
},
{
"cell_type": "markdown",
"id": "2810414a",
"metadata": {},
"source": [
"##### print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69728fa8",
"metadata": {},
"outputs": [],
"source": [
"print(string)\n",
"print(a_list)\n",
"print(dictionary)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cf3305b",
"metadata": {},
"outputs": [],
"source": [
"# We have an alternative way of setting up our string\n",
"pi_to_9_decimal_places = 3.141592653\n",
"another_string = f\"pi to 9 d.p.s is {pi_to_9_decimal_places}\"\n",
"print( another_string )"
]
},
{
"cell_type": "markdown",
"id": "44a2adb7",
"metadata": {},
"source": [
"#### round"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4a16deb",
"metadata": {},
"outputs": [],
"source": [
"pi_to_9_decimal_places = 3.141592653\n",
"pi_to_6_decimal_places = round(pi_to_9_decimal_places,6)\n",
"\n",
"another_string = f\"pi to 6 d.p.s is { pi_to_6_decimal_places }\"\n",
"print( another_string )"
]
},
{
"cell_type": "markdown",
"id": "8c62b6a3",
"metadata": {},
"source": [
"#### len"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8210cc5",
"metadata": {},
"outputs": [],
"source": [
"len(a_list)"
]
},
{
"cell_type": "markdown",
"id": "c10470cb",
"metadata": {},
"source": [
"#### count"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3707bd97",
"metadata": {},
"outputs": [],
"source": [
"yet_another_string = \"LHCb is the best experiment , LHCb does b and c physics\"\n",
"yet_another_string.count(\"LHCb\")"
]
},
{
"cell_type": "markdown",
"id": "78b9426f",
"metadata": {},
"source": [
"##### sum"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c827e9f6",
"metadata": {},
"outputs": [],
"source": [
"yet_another_list = [ 1 , 2 , 3 ]\n",
"sum(yet_another_list)"
]
},
{
"cell_type": "markdown",
"id": "532615b6",
"metadata": {},
"source": [
"#### type"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0983f8f0",
"metadata": {},
"outputs": [],
"source": [
"type(string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6d6a3c1",
"metadata": {},
"outputs": [],
"source": [
"type(a_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03d4837c",
"metadata": {},
"outputs": [],
"source": [
"type(dictionary)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bee24af4",
"metadata": {},
"outputs": [],
"source": [
"type(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a02d53fc",
"metadata": {},
"outputs": [],
"source": [
"# We have different types\n",
"int\n",
"dict\n",
"list\n",
"str"
]
},
{
"cell_type": "markdown",
"id": "a67993ab",
"metadata": {},
"source": [
"## basic operations"
]
},
{
"cell_type": "markdown",
"id": "512292e3",
"metadata": {},
"source": [
"#### $ + , - , * , / $"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "185e6c2a",
"metadata": {},
"outputs": [],
"source": [
"a = 2\n",
"b = 4\n",
"print(\"a+b = \" + str(a + b) )\n",
"print(\"a-b = \" + str(a - b) )\n",
"print(\"a*b = \" + str(a * b) )\n",
"print(\"a/b = \" + str(a / b) )\n"
]
},
{
"cell_type": "markdown",
"id": "6cf53c3a",
"metadata": {},
"source": [
"#### % - modulo"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68963a3c",
"metadata": {},
"outputs": [],
"source": [
"3 % 2 # 3 modulo 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0ef55c7",
"metadata": {},
"outputs": [],
"source": [
"5 % 5 # 5 modulo 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39318b5e",
"metadata": {},
"outputs": [],
"source": [
"19 % 20 # 19 modulo 20"
]
},
{
"cell_type": "markdown",
"id": "6d14fb40",
"metadata": {},
"source": [
"#### =="
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "ed3e510e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A == B ? False\n"
]
}
],
"source": [
"A = 1\n",
"B = 2\n",
"print(\"A == B ? \" + str(A == B) )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8ff36b6",
"metadata": {},
"outputs": [],
"source": [
"A = 1\n",
"print(\"A is an integer ? \" + str( type(A) == int) )"
]
},
{
"cell_type": "markdown",
"id": "a268e18e",
"metadata": {},
"source": [
"#### !="
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5fa376b",
"metadata": {},
"outputs": [],
"source": [
"A = 1\n",
"B = 2\n",
"print(\"A = \" + str(A))\n",
"print(\"B = \" + str(B))\n",
"print(\"A != B \" + str(A != B) )\n",
"C = 2\n",
"D = 2\n",
"print(\"C = \" + str(C))\n",
"print(\"D = \" + str(D))\n",
"print(\"C != D \" + str(C != D) )"
]
},
{
"cell_type": "markdown",
"id": "e09da64e",
"metadata": {},
"source": [
"#### $ > , < $"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88f65055",
"metadata": {},
"outputs": [],
"source": [
"A = 1\n",
"B = 2\n",
"print(\"A = \" + str(A))\n",
"print(\"B = \" + str(B))\n",
"\n",
"print(\"A > B \" + str(A > B) )\n",
"print(\"A < B \" + str(A < B) )\n",
"print(\"A >= B \" + str(A >= B) )\n",
"print(\"A <= B \" + str(A <= B) )"
]
},
{
"cell_type": "markdown",
"id": "330b9950",
"metadata": {},
"source": [
"#### $ >= , <= $"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6302e254",
"metadata": {},
"outputs": [],
"source": [
"A = 2\n",
"B = 2\n",
"print(\"A = \" + str(A))\n",
"print(\"B = \" + str(B))\n",
"\n",
"print(\"A > B \" + str(A > B) )\n",
"print(\"A < B \" + str(A < B) )\n",
"print(\"A >= B \" + str(A >= B) )\n",
"print(\"A <= B \" + str(A <= B) )"
]
},
{
"cell_type": "markdown",
"id": "ca780f02",
"metadata": {},
"source": [
"#### for"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce822260",
"metadata": {},
"outputs": [],
"source": [
"# for loops\n",
"print(\"First for loop\")\n",
"list_of_values = [ 1 , 2 , 3 ]\n",
"for element_of_list in list_of_values:\n",
" print(element_of_list)\n",
"\n",
"print(\"for loop using range function\")\n",
"# another way of making a list\n",
"list_of_value = range( 0 , 3 )\n",
"for element_of_list in list_of_values:\n",
" print(element_of_list)"
]
},
{
"cell_type": "markdown",
"id": "3506e4bb",
"metadata": {},
"source": [
"#### if & else"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f78c4ad2",
"metadata": {},
"outputs": [],
"source": [
"A = [ 1 , 2 , 1 , 1 ]\n",
"\n",
"for element in A:\n",
" if element == 1:\n",
" print(\"element is 1\")\n",
" else:\n",
" print(\"element is not 1\") \n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"id": "7723b2dc",
"metadata": {},
"source": [
"#### elif"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7751c7b",
"metadata": {},
"outputs": [],
"source": [
"A = [ 1 , 2 , 3 , 1 ]\n",
"\n",
"for element in A:\n",
" if element == 1:\n",
" print(\"element is 1\")\n",
" elif element ==2:\n",
" print(\"element is 2\")\n",
" else:\n",
" print(\"element is not 1 or 2\") "
]
},
{
"cell_type": "markdown",
"id": "6c265c0f",
"metadata": {},
"source": [
"#### while"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "856f24db",
"metadata": {},
"outputs": [],
"source": [
"i = 1\n",
"while i < 5:\n",
" print(i)\n",
" i += 1 # i goes to i + 1 "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b3a312a",
"metadata": {},
"outputs": [],
"source": [
"i = 1\n",
"while True:\n",
" print(i)\n",
" i += 1 # i goes to i + 1\n",
" \n",
" if i >= 5:\n",
" break"
]
},
{
"cell_type": "markdown",
"id": "57692d8f",
"metadata": {},
"source": [
"# Challenge for you"
]
},
{
"cell_type": "markdown",
"id": "e9bd75ed",
"metadata": {},
"source": [
"### write a python script, that checks if a number is prime or not"
]
},
{
"cell_type": "markdown",
"id": "fe6f44a2",
"metadata": {},
"source": [
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*\n",
"\n",
"*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "72a86139",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number 7 is a prime number\n"
]
}
],
"source": [
"number = 7\n",
"\n",
"i = 2\n",
"while i <= number:\n",
" if i == number:\n",
" print(f\"number {number} is a prime number\")\n",
" break\n",
" \n",
" else:\n",
" if number % i == 0:\n",
" print(f\"number {number} is not a prime number\")\n",
" break\n",
" \n",
" i += 1"
]
},
{
"cell_type": "markdown",
"id": "c3ad43f7",
"metadata": {},
"source": [
"## Modules"
]
},
{
"cell_type": "markdown",
"id": "41740308",
"metadata": {},
"source": [
"Python is open sources and many people have made modules for all kind of things\n",
"There are some important modules for science:\n",
"- Numpy - https://numpy.org/doc/stable/user/quickstart.html\n",
"- Scipy - https://docs.scipy.org/doc/scipy/tutorial/index.html\n",
"- Matplotlib - https://matplotlib.org/stable/tutorials/index.html\n",
"\n",
"These are external to python and may need to be installed, which you can do in your bash shell by using\n",
"pip install numpy\n",
"pip install scipy\n",
"pip install matplotlib\n",
"\n",
"Before we look at them, let's look at some libraries/modules that come with python"
]
},
{
"cell_type": "markdown",
"id": "0dcd1688",
"metadata": {},
"source": [
"#### time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "60b26e58",
"metadata": {},
"outputs": [],
"source": [
"# first let's import the module\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2bd72c2e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1666962508.9378667\n"
]
}
],
"source": [
"# we now have a module, what can we do with it\n",
"print(time.time())\n",
"# gives a time right now"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "703a7bea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"begin\n",
"time elapsed (units of seconds) = 5.005500793457031\n"
]
}
],
"source": [
"print(\"begin\")\n",
"start = time.time()\n",
"time.sleep(5) # sleep for 5 seconds\n",
"end = time.time()\n",
"print(f\"time elapsed (units of seconds) = {end - start}\")\n"
]
},
{
"cell_type": "markdown",
"id": "3854437d",
"metadata": {},
"source": [
"### So we can import a module, time, and then access functions within using time.function"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "25418eec",
"metadata": {},
"outputs": [],
"source": [
"import numpy"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "f6553899",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"numpy.linspace( 0 , 100 , 20 ) = [ 0. 5.26315789 10.52631579 15.78947368 21.05263158\n",
" 26.31578947 31.57894737 36.84210526 42.10526316 47.36842105\n",
" 52.63157895 57.89473684 63.15789474 68.42105263 73.68421053\n",
" 78.94736842 84.21052632 89.47368421 94.73684211 100. ]\n"
]
}
],
"source": [
"#### make a list using numpy\n",
"### 20 numbers linearly spaced between 1 and 100\n",
"array = numpy.linspace( 0 , 100 , 20 )\n",
"print(f\"numpy.linspace( 0 , 100 , 20 ) = {array}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "45f91306",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"numpy.arange( 0 , 100 , 20 ) = [ 0 20 40 60 80]\n"
]
}
],
"source": [
"### if we wanted a specific spacing\n",
"### this is with a spacing of 5\n",
"array = numpy.arange( 0 , 100 , 20 )\n",
"print(f\"numpy.arange( 0 , 100 , 20 ) = {array}\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "171e819e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"np.arange( 0 , 100 , 20 ) = [ 0 20 40 60 80]\n"
]
}
],
"source": [
"### note we don't have to call it numpy\n",
"\n",
"import numpy as np\n",
"\n",
"array = np.arange( 0 , 100 , 20 )\n",
"print(f\"np.arange( 0 , 100 , 20 ) = {array}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa89cf85",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
},
"tags": [
"hide-cell"
]
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:ffded810 tags:
# LHCb Starterkit - Introduction to python - part 1
-
modules
-
running other python scripts
%% Cell type:markdown id:10212d4a tags:
## basic obejcts
%% Cell type:code id:f8082fd8 tags:
```
python
string
=
"
Hello World
"
a_list
=
[
1
,
5.04
,
string
]
dictionary
=
{
"
element 1
"
:
1
,
"
element 2
"
:
a_list
,
"
element 3
"
:
string
}
```
%% Cell type:markdown id:aef29764 tags:
## basic functions
%% Cell type:markdown id:2810414a tags:
##### print
%% Cell type:code id:69728fa8 tags:
```
python
print
(
string
)
print
(
a_list
)
print
(
dictionary
)
```
%% Cell type:code id:1cf3305b tags:
```
python
# We have an alternative way of setting up our string
pi_to_9_decimal_places
=
3.141592653
another_string
=
f
"
pi to 9 d.p.s is
{
pi_to_9_decimal_places
}
"
print
(
another_string
)
```
%% Cell type:markdown id:44a2adb7 tags:
#### round
%% Cell type:code id:e4a16deb tags:
```
python
pi_to_9_decimal_places
=
3.141592653
pi_to_6_decimal_places
=
round
(
pi_to_9_decimal_places
,
6
)
another_string
=
f
"
pi to 6 d.p.s is
{
pi_to_6_decimal_places
}
"
print
(
another_string
)
```
%% Cell type:markdown id:8c62b6a3 tags:
#### len
%% Cell type:code id:d8210cc5 tags:
```
python
len
(
a_list
)
```
%% Cell type:markdown id:c10470cb tags:
#### count
%% Cell type:code id:3707bd97 tags:
```
python
yet_another_string
=
"
LHCb is the best experiment , LHCb does b and c physics
"
yet_another_string
.
count
(
"
LHCb
"
)
```
%% Cell type:markdown id:78b9426f tags:
##### sum
%% Cell type:code id:c827e9f6 tags:
```
python
yet_another_list
=
[
1
,
2
,
3
]
sum
(
yet_another_list
)
```
%% Cell type:markdown id:532615b6 tags:
#### type
%% Cell type:code id:0983f8f0 tags:
```
python
type
(
string
)
```
%% Cell type:code id:e6d6a3c1 tags:
```
python
type
(
a_list
)
```
%% Cell type:code id:03d4837c tags:
```
python
type
(
dictionary
)
```
%% Cell type:code id:bee24af4 tags:
```
python
type
(
2
)
```
%% Cell type:code id:a02d53fc tags:
```
python
# We have different types
int
dict
list
str
```
%% Cell type:markdown id:a67993ab tags:
## basic operations
%% Cell type:markdown id:512292e3 tags:
#### $ + , - , * , / $
%% Cell type:code id:185e6c2a tags:
```
python
a
=
2
b
=
4
print
(
"
a+b =
"
+
str
(
a
+
b
)
)
print
(
"
a-b =
"
+
str
(
a
-
b
)
)
print
(
"
a*b =
"
+
str
(
a
*
b
)
)
print
(
"
a/b =
"
+
str
(
a
/
b
)
)
```
%% Cell type:markdown id:6cf53c3a tags:
#### % - modulo
%% Cell type:code id:68963a3c tags:
```
python
3
%
2
# 3 modulo 2
```
%% Cell type:code id:d0ef55c7 tags:
```
python
5
%
5
# 5 modulo 5
```
%% Cell type:code id:39318b5e tags:
```
python
19
%
20
# 19 modulo 20
```
%% Cell type:markdown id:6d14fb40 tags:
#### ==
%% Cell type:code id:ed3e510e tags:
```
python
A
=
1
B
=
2
print
(
"
A == B ?
"
+
str
(
A
==
B
)
)
```
%% Output
A == B ? False
%% Cell type:code id:e8ff36b6 tags:
```
python
A
=
1
print
(
"
A is an integer ?
"
+
str
(
type
(
A
)
==
int
)
)
```
%% Cell type:markdown id:a268e18e tags:
#### !=
%% Cell type:code id:b5fa376b tags:
```
python
A
=
1
B
=
2
print
(
"
A =
"
+
str
(
A
))
print
(
"
B =
"
+
str
(
B
))
print
(
"
A != B
"
+
str
(
A
!=
B
)
)
C
=
2
D
=
2
print
(
"
C =
"
+
str
(
C
))
print
(
"
D =
"
+
str
(
D
))
print
(
"
C != D
"
+
str
(
C
!=
D
)
)
```
%% Cell type:markdown id:e09da64e tags:
#### $ > , < $
%% Cell type:code id:88f65055 tags:
```
python
A
=
1
B
=
2
print
(
"
A =
"
+
str
(
A
))
print
(
"
B =
"
+
str
(
B
))
print
(
"
A > B
"
+
str
(
A
>
B
)
)
print
(
"
A < B
"
+
str
(
A
<
B
)
)
print
(
"
A >= B
"
+
str
(
A
>=
B
)
)
print
(
"
A <= B
"
+
str
(
A
<=
B
)
)
```
%% Cell type:markdown id:330b9950 tags:
#### $ >= , <= $
%% Cell type:code id:6302e254 tags:
```
python
A
=
2
B
=
2
print
(
"
A =
"
+
str
(
A
))
print
(
"
B =
"
+
str
(
B
))
print
(
"
A > B
"
+
str
(
A
>
B
)
)
print
(
"
A < B
"
+
str
(
A
<
B
)
)
print
(
"
A >= B
"
+
str
(
A
>=
B
)
)
print
(
"
A <= B
"
+
str
(
A
<=
B
)
)
```
%% Cell type:markdown id:ca780f02 tags:
#### for
%% Cell type:code id:ce822260 tags:
```
python
# for loops
print
(
"
First for loop
"
)
list_of_values
=
[
1
,
2
,
3
]
for
element_of_list
in
list_of_values
:
print
(
element_of_list
)
print
(
"
for loop using range function
"
)
# another way of making a list
list_of_value
=
range
(
0
,
3
)
for
element_of_list
in
list_of_values
:
print
(
element_of_list
)
```
%% Cell type:markdown id:3506e4bb tags:
#### if & else
%% Cell type:code id:f78c4ad2 tags:
```
python
A
=
[
1
,
2
,
1
,
1
]
for
element
in
A
:
if
element
==
1
:
print
(
"
element is 1
"
)
else
:
print
(
"
element is not 1
"
)
```
%% Cell type:markdown id:7723b2dc tags:
#### elif
%% Cell type:code id:b7751c7b tags:
```
python
A
=
[
1
,
2
,
3
,
1
]
for
element
in
A
:
if
element
==
1
:
print
(
"
element is 1
"
)
elif
element
==
2
:
print
(
"
element is 2
"
)
else
:
print
(
"
element is not 1 or 2
"
)
```
%% Cell type:markdown id:6c265c0f tags:
#### while
%% Cell type:code id:856f24db tags:
```
python
i
=
1
while
i
<
5
:
print
(
i
)
i
+=
1
# i goes to i + 1
```
%% Cell type:code id:1b3a312a tags:
```
python
i
=
1
while
True
:
print
(
i
)
i
+=
1
# i goes to i + 1
if
i
>=
5
:
break
```
%% Cell type:markdown id:57692d8f tags:
# Challenge for you
%% Cell type:markdown id:e9bd75ed tags:
### write a python script, that checks if a number is prime or not
%% Cell type:markdown id:fe6f44a2 tags:
*
*
*
*
*
*
*
*
*
*
*
*
%% Cell type:code id:72a86139 tags:
```
python
number
=
7
i
=
2
while
i
<=
number
:
if
i
==
number
:
print
(
f
"
number
{
number
}
is a prime number
"
)
break
else
:
if
number
%
i
==
0
:
print
(
f
"
number
{
number
}
is not a prime number
"
)
break
i
+=
1
```
%% Output
number 7 is a prime number
%% Cell type:markdown id:c3ad43f7 tags:
## Modules
%% Cell type:markdown id:41740308 tags:
Python is open sources and many people have made modules for all kind of things
There are some important modules for science:
-
Numpy - https://numpy.org/doc/stable/user/quickstart.html
-
Scipy - https://docs.scipy.org/doc/scipy/tutorial/index.html
-
Matplotlib - https://matplotlib.org/stable/tutorials/index.html
These are external to python and may need to be installed, which you can do in your bash shell by using
pip install numpy
pip install scipy
pip install matplotlib
Before we look at them, let's look at some libraries/modules that come with python
%% Cell type:markdown id:0dcd1688 tags:
#### time
%% Cell type:code id:60b26e58 tags:
```
python
# first let's import the module
import
time
```
%% Cell type:code id:2bd72c2e tags:
```
python
# we now have a module, what can we do with it
print
(
time
.
time
())
# gives a time right now
```
%% Output
1666962508.9378667
%% Cell type:code id:703a7bea tags:
```
python
print
(
"
begin
"
)
start
=
time
.
time
()
time
.
sleep
(
5
)
# sleep for 5 seconds
end
=
time
.
time
()
print
(
f
"
time elapsed (units of seconds) =
{
end
-
start
}
"
)
```
%% Output
begin
time elapsed (units of seconds) = 5.005500793457031
%% Cell type:markdown id:3854437d tags:
### So we can import a module, time, and then access functions within using time.function
%% Cell type:code id:25418eec tags:
```
python
import
numpy
```
%% Cell type:code id:f6553899 tags:
```
python
#### make a list using numpy
### 20 numbers linearly spaced between 1 and 100
array
=
numpy
.
linspace
(
0
,
100
,
20
)
print
(
f
"
numpy.linspace( 0 , 100 , 20 ) =
{
array
}
"
)
```
%% Output
numpy.linspace( 0 , 100 , 20 ) = [ 0. 5.26315789 10.52631579 15.78947368 21.05263158
26.31578947 31.57894737 36.84210526 42.10526316 47.36842105
52.63157895 57.89473684 63.15789474 68.42105263 73.68421053
78.94736842 84.21052632 89.47368421 94.73684211 100. ]
%% Cell type:code id:45f91306 tags:
```
python
### if we wanted a specific spacing
### this is with a spacing of 5
array
=
numpy
.
arange
(
0
,
100
,
20
)
print
(
f
"
numpy.arange( 0 , 100 , 20 ) =
{
array
}
"
)
```
%% Output
numpy.arange( 0 , 100 , 20 ) = [ 0 20 40 60 80]
%% Cell type:code id:171e819e tags:
```
python
### note we don't have to call it numpy
import
numpy
as
np
array
=
np
.
arange
(
0
,
100
,
20
)
print
(
f
"
np.arange( 0 , 100 , 20 ) =
{
array
}
"
)
```
%% Output
np.arange( 0 , 100 , 20 ) = [ 0 20 40 60 80]
%% Cell type:code id:fa89cf85 tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment