This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpython_basics.py
93 lines (65 loc) · 1.57 KB
/
python_basics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
This script will demonstrate some of the basic operations in
python, like if statments, for loops, and functions.
"""
#%%
# This is a comment. Comments are not executed by the computer.
# This is a variable. Variables are used to store information.
# Variables can be numbers, strings, lists, or other things.
# Variables can be changed.
x = 1
# %% create a basic function
def new_function():
pass
#%% run the new function
new_function()
# %% function to print stuff
def func2(arg1):
"""
This function accepts an argument and
prints out that argument.
"""
print("The argument is ", arg1)
#%% run func2
func2('Hello World')
func2()
# %%
def func3(arg1, arg2=True):
if arg2:
print('arg2 is True!')
print('arg1 is', arg1)
#%% run func3
func3('test')
func3('test2', False)
# use named arguments
print('example with named args:')
func3(arg2=True, arg1=123)
# %% add numbers
def add_numbers(a, b):
"""
This function adds two numbers together
"""
result = a + b
return result
#%%
c = add_numbers(2, 123)
# %% test cases
assert add_numbers(2, 2) == 4
assert add_numbers(4, -1) == 3
assert add_numbers(3.2, 1.4) == 4.6
# %% lab preparation
my_list = ['Hello', 'BFOR', 206, None, 'Bye!']
# %% for loop over the list
for item in my_list:
print(item)
# %% hint for today's lab
a=None
if a:
print('a has a value:', a)
else:
print('a is None')
# %% demonstrating numeric index for loop
for i in range(len(my_list)):
print("the value of i is", i)
print("the value of my_list at i is", my_list[i])
# %%