python

Continue Statements

python

continue statements are used inside of while loops to jump back to the start of the loop and reevaluate the loop’s condition . In for loop’s it jumps to the next iteration of the loop. Example # while True: print('Who are you?') name = input() if name != 'Joe': continue print('Hello, Joe. What is the password? (It is a fish.)') password = input() if password == 'swordfish': break print('Access granted. ...

Converting Types With list() and tuple()

python

The list() and tuple() functions will return list and tuple versions of the values passed to them. >>> tuple(['cat', 'dog', 5]) ('cat', 'dog', 5) >>> list(('cat', 'dog', 5)) ['cat', 'dog', 5] >>> list('hello') ['h', 'e', 'l', 'l', 'o']

copy Module

python

copy() # Used to make a copy of mutable data like lists or dictionaries. >>> spam = ['A', 'B', 'C', 'D'] >>> id(spam) 140619514272768 >>> cheese = copy.copy(spam) >>> id(cheese) 140619515247488 >>> cheese[1] = 42 >>> spam ['A', 'B', 'C', 'D'] >>> cheese ['A', 42, 'C', 'D'] If the lists contains lists use copy.deepcopy() to copy those inner lists as well.

Creating and Using a Class

python

A class is an object that can be used to store data or do a thing. class Dog: """A simple attempt to model a dog.""" def __init__(self, name, age): """Initialize name and age attributes.""" self.name = name self.age = age def sit(self): """Simulate a dog sitting in response to a command.""" print(f"{self.name}is now sitting.") def roll_over(self): """Simulate rolling over in response to a command.""" print(f"{self.name}rolled over!") Convention is to capitalize Names that refer to classes. ...

Def Statement

python

def statement defines a function with a name. Can accept arguments . See: Parameters Example: def hello(name): print('Hello, ' + name) hello('Alice') Define, Call, Pass, Argument, Parameter # The first line of the example above defines the function. The name variable is the parameter that an argument is passed to. The argument ‘Alice’ is passed to the function when the function is called. When the function is called ‘Alice’ is assigned to the name parameter. ...

del Statement

python

You can delete values at an index in a list with the del statement. >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> del spam[2] >>> spam ['cat', 'bat', 'elephant'] >>> del spam[2] >>> spam ['cat', 'bat']

Dictionary Data Type

python

A dictionary is a mutable collection of many values. Values are associated with keys. Unlike lists , dictionaries aren’t indexed and keys can be in any order. Dictionaries are a collection of key-value pairs. Integers or strings can be used for keys. >>> myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'} >>> myCat {'size': 'fat', 'color': 'gray', 'disposition': 'loud'} >>> myCat['size'] 'fat' >>> 'My cat has ' + myCat['color'] + ' fur. ...

Elif Statements

python

The elif statement is an “else if” statement following either an if statement or another elif statement. It’s condition is checked only if all previous conditions were False. An elif statement consists of the following: The elif keyword A condition A colon Starting on the next line, an indented block of code if name == 'Alice': print('Hi, Alice.') elif age < 12: print('You are not Alice, kiddo.') else: print('Hello, stranger. ...

Else Statements

python

An if clause can optionally be followed by an else Statement. The else clause is executed only when the if statement’s condition is False. else statement always consists of the following: The else keyword A colon Starting on the next line, and indented block of code if name == 'Alice': print('Hi, Alice.') else: print('Hello, stranger.')