python

Global Statements

python

You can modify a global variable from within a function with a global statement. _Note: this isn’t done very often. It’s better to be able to treat functions as black boxes. def spam(): global eggs # <- global statement eggs = 'spam' eggs = 'global' spam() print(eggs) Output: spam

id

python

The numeric address where a value is stored can be gotten with the id() function. >>> id('Howdy') 139712390384112 Strings are immutable and if you change a string a new string object is created in memory. >>> bacon = 'Hello' >>> id(bacon) 139712390384240 >>> bacon += ' world!' # A new string is born >>> id(bacon) 139712390384368 # bacon now refers to a different string Note, Python has automatic garbage collection which deletes any values not being referred to by any variables to free up memory. ...

If Statements

python

An if statement will execute the clause when it’s condition evaluates to True. If this condition is true, execute the code in the clause. If statements consist of: The if keyword A condition A colon Starting on the next line, and indented block of code. if name == 'Alice': print('Hi, Alice.')

Immutable Data Type

python

Immutable data types are data that’s unchangeable. Strings and tuples are examples of immutable data types. It means the value of the data can’t be changed in place. Lists are an example of a data type that can be changed and is mutable .

Importing Classes

python

Just like with builtin modules or modules that other’s have build you can import your own groups of classes the same way. Importing Classes # Importing a single class # Always include a docstring at the head of a module you create. You can import modules just like with any other module. from car import Car my_new_car = Car('audi', 'a4', 2019) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer() 2019 Audi A4 This car has 23 miles on it. ...

Importing Modules

python

import statement consists of the following: The import keyword The name of the module Optionally, more module names, as long as they are separated by commas Example: import random for i in range(5): print(random.randint(1, 10)) Note: don’t overwrite module names like os, sys, random, etc. Alternative form: from random import * Importing multiple modules: import random, sys, os, math

in and not in Operators

python

You can determine whether a value is in or not in a list . Returns True or False. >>> spam = ['hello', 'hi', 'howdy', 'heyas'] >>> 'cat' in spam False >>> 'howdy' not in spam False >>> 'cat' not in spam True You can also use the in and not operators to check if a key or value exists in a dict . >>> spam = {'name': 'Zophie', 'age': 7} >>> 'name' in spam. ...

in and not in String Operators

python

Just like with lists you can use the in and not in operators to check if something is in a string. >>> 'Hello' in 'Hello, World' True >>> 'HELLO' in 'Hello, World' False >>> '' in 'spam' True >>> 'cats' not in 'cats and dogs' False

Index

python

An index is the position number of an item in a list Must be an integer For example: >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[0] 'cat' >>> spam[1] 'bat' >>> spam[2] 'rat' >>> spam[3] 'elephant' >>> ['cat', 'bat', 'rat', 'elephant'][3] 'elephant' Negative indexes # Lists also have negative indexes. You can grab an item from a list like so: >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[-2] 'rat' You’ll notice that negative indexes start at -1 (and not -0! ...