Sie sind auf Seite 1von 1

Python Control Flow Week 3 Reference 2018-Spring

Types Branching Functions

str Basic if Optionally execute indented Positional parameters


code based on the truth value of
a = "hello!" the condition def add(a, b):
a.upper() # HELLO! c = a + b
a.capitalize() # Hello! if cost < 10: print("the sum is", c)
a.strip("!") # hello print("impulse buy") add(1, 2)
a.index("e") # 2
a.split("e") # ["h", "llo!"] Boolean operators “and”, “or” Keyword parameters
number types if age > 17 and place == "UK":
print("can buy alcohol") def greet(name="Jack"):
count = 3 # int if age < 18 or s == "student": print("Hello", name)
pi = 3.14 # float print("can get discount") greet(name="Jill")

list If-elif-else Return value

a = ["a", "b", 3] if beer == "Darkwing": def in_file(name):


a[0] # "a" print("IPA") path = "./src/" + name
a[1] # "b" elif beer == "Hefe": return path + ".html"
a[-1] # "3" print("Hefeweizen") path = in_file("home")
a[1:2] # ["b", 3] elif beer == "Stonehenge": html = open(path).read()
print("Stout")
tuple same as list, but immutable else: Comment aka “docstring”
print("Unknown beer")
a = ("a", "b", 3) def plural(word):
Pass placeholder that does nothing """
dict Return the plural of
if cost > 1.99: an English word.
a = {"a": 1, "b": 2} pass # TODO: finish this """
a["a"] # 1 if word.endswith("s"):
a["b"] # 2 return word + "es"
del ["a"] # deletes "a" Iteration return word + "s"
a["c"] = 3 # adds "c" to dict print("Many", plural("cat"))
list methods For loop Execute the indented code for
each item in a list or other “it- Lambda alternative syntax for one-
a = ["a", "b", 3] erable”, temporarily putting that liners
a.append(4) # ["a", "b", 3, 4] item in a given variable
a.reverse() # [4, 3, "b", "a"] cubed = lambda i: i ** 3
names = ["John", "Paul", "G"] print("5^3 is ", cubed(5))
dict methods for name in names:
print("name:", name)
a = {"a": 1, "b": 2} Programming Concepts
a.get("c", 3) # 3 as default Range for-loop Useful for looping
a.update({"d": 4}) # add more through numbers
a.keys() # iterable of keys Pseudocode A “rough draft” in fake
a.values() # ... of values for x in range(0, 100): coding that serves as a middle-
a.items() # ... of both print("x:", x) ground in the process of converting
high-level thought into lower-level
While loop Repeat indented code un- code.
til condition is no longer true
Key Terms State diagram Diagramming what
i = 2 different variables should hold at
while i < 10000: different times in the execution of
Variable A named “bucket” that you print("square:", i)
can put data into. your application.
i = i ** 2
Assignment The act of putting data Interruption Exit loops prematurely Scope The idea that variables assigned
into a variable. within functions aren’t accessible
with break, skip to next iteration outside that function.
Function A bit of code given a name. with continue
Functions attached to objects are Refactor The process of converting
for i in range(0, 50): ugly, repetitive code into clean
called “methods”. choice = input("quit/skip? ") code which follows D.R.Y. (Don’t
Call To cause a function to run, also if choice == "quit":
break Repeat Yourself) principles.
known as “invoking” the function.
elif choice == "skip":
Arguments Data provided to a func- continue
tion when calling a function. print("i", i, "i^2", i ** 2)

Kickstart Coding http://kickstartcoding.com/

Das könnte Ihnen auch gefallen