8000 Added args, scope, and tuples · pythonminna/Intro-Python@2414b09 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 2414b09

Browse files
author
Beej Jorgensen
committed
Added args, scope, and tuples
1 parent bad2878 commit 2414b09

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ Suggested order for implementing the toy programs:
8181
* `modules` -- Learn to import from modules
8282
* `printf` -- Formatted print output
8383
* `lists` -- Python's version of arrays
84+
* `tuples` -- Immutable lists typically for heterogenous data
8485
* `slice` -- Accessing parts of lists
8586
* `comp` -- List comprehensions
8687
* `dicts` -- Dictionaries
8788
* `func` -- Functions
8889
* `args` -- Arguments and Keyword Arguments
90+
* `scope` -- Global, Local, and Non-Local scope
8991
* `fileio` -- Read and write from files
9092
* `cal` -- Experiment with module imports
9193
* `obj` -- Classes and objects

src/args.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Experiment with positional arguments, arbitrary arguments, and keyword
2+
# arguments.
3+
4+
# Write a function f1 that takes two integer positional arguments and returns
5+
# the sum. This is what you'd consider to be a regular, normal function.
6+
7+
#def f1(...
8+
9+
print(f1(1, 2))
10+
11+
# Write a function f2 that takes any number of iteger arguments and prints the
12+
# sum. Google for "python arbitrary arguments" and look for "*args"
13+
14+
# def f2(...
15+
16+
print(f2(1)) # Should print 1
17+
print(f2(1, 3)) # Should print 4
18+
print(f2(1, 4, -12)) # Should print -7
19+
print(f2(7, 9, 1, 3, 4, 9, 0)) # Should print 33
20+
21+
a = [7, 6, 5, 4]
22+
23+
# What thing do you have to add to make this work?
24+
print(f2(a)) # Should print 22
25+
26+
# Write a function f3 that accepts either one or two arguments. If one argument,
27+
# it returns that value plus 1. If two arguments, it returns the sum of the
28+
# arguments. Google "python default arguments" for a hint.
29+
30+
#def f3(...
31+
32+
print(f3(1, 2)) # Should print 3
33+
print(f3(8)) # Should print 9
34+
35+
36+
# Write a function f4 that accepts an arbitrary number of keyword arguments and
37+
# prints ouf the keys and values like so:
38+
#
39+
# key: foo, value: bar
40+
# key: baz, value: 12
41+
#
42+
# Google "python keyword arguments".
43+
44+
#def f4(...
45+
46+
# Should print
47+
# key: a, value: 12
48+
# key: b, value: 30
49+
f4(a=12, b=30)
50+
51+
# Should print
52+
# key: city, value: Berkeley
53+
# key: population, value: 121240
54+
# key: founded, value: "March 23, 1868"
55+
f4(city="Berkeley", population=121240, founded="March 23, 1868")
56+
57+
d = {
58+
"monster": "goblin",
59+
"hp": 3
60+
}
61+
62+
# What thing do you have to add to make this work?
63+
f4(d)

src/scope.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Experiment with scope in Python.
2+
# Good reading: https://www.programiz.com/python-programming/global-local-nonlocal-variables
3+
4+
# When you use a variable in a function, it's local in scope to the function.
5+
x = 12
6+
7+
def changeX():
8+
x = 99
9+
10+
changeX()
11+
12+
# This prints 12. What do we have to modify in changeX() to get it to print 99?
13+
print(x)
14+
15+
16+
# This nested function has a similar problem.
17+
18+
def outer():
19+
y = 120
20+
21+
def inner():
22+
y = 999
23+
24+
inner()
25+
2 6D40 6+
# This prints 120. What do we have to change in inner() to get it to print
27+
# 999? Google "python nested function scope".
28+
print(y)
29+
30+
outer()

src/tuples.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Tuples are like lists, but are immutable and are usually used to hold
2+
# heterogenous data. They use parens instead of square brackets.
3+
4+
# Example:
5+
6+
import math
7+
8+
def dist(a, b):
9+
"""Compute the distance between two x,y points."""
10+
x0, y0 = a # Destructuring assignment
11+
x1, y1 = b
12+
13+
return math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
14+
15+
a = (2, 7) # <-- x,y coordinates stored in tuples
16+
b = (-14, 72)
17+
18+
# Prints "Distance is 66.94"
19+
print("Distance is: {:.2f}".format(dist(a, b)))
20+
21+
22+
23+
# Write a function that prints all the values in a tuple
24+
25+
# def print_tuple(...
26+
27+
t = (1, 2, 5, 7, 99)
28+
print_tuple(t) # Prints 1 2 5 7 99, one per line
29+
30+
# Declare a tuple of 1 element then print it
31+
u = (1) # What needs to be added to make this work?
32+
print_tuple(u)

0 commit comments

Comments
 (0)
0