Skip to content

Instantly share code, notes, and snippets.

@G-Goldstein
Created June 17, 2016 13:01
Show Gist options
  • Save G-Goldstein/46c2ef4f58372e4fdfe65b62aabb7c65 to your computer and use it in GitHub Desktop.
Save G-Goldstein/46c2ef4f58372e4fdfe65b62aabb7c65 to your computer and use it in GitHub Desktop.
# Lists
numbers = [3, 5, 7, 4, 2]
## Don't worry about remembering the syntax, as long as you can refer back to an example.
print(numbers)
print(max(numbers))
print(len(numbers))
## Lists can contain anything - even other lists!
fruits = ['Apple', 'Pear', 'Orange', 'Banana']
print(fruits)
# 'for' loops
for fruit in fruits:
print(fruit)
## In the above, the word 'fruit' is just a new variable. We could use anything:
for x in fruits:
print(x)
for vegetable in fruits:
print(vegetable)
## But it's best to use 'fruit' here because that's what they are!
# Example: Given a list of release codes, present them as release titles:
release_codes = ['142', '143', '144', '151', '152', '153', '154', '161', '162', '163']
## For '142', we want to print 'Release F142', and so on
for release_code in release_codes:
print('Release F' + release_code)
# Challenge 1: Given the below list of software package numbers, print out just those packages over 30000
software_packages = [27588, 30556, 28321, 30322, 26019, 30028, 25236]
## Hint - Recall how we used 'if' statements before to make decisions.
# Andrew Westwood:
for SP in software_packages:
if SP > 30000:
print(SP)
# We can also use 'for comprehensions' to create new lists from old lists.
# In this example, we create a new list of just those SPs with numbers over 30000.
# First we define a function to use as our condition.
def over_30000(sp):
if sp > 30000
return True
else:
return False
# Then we can put the whole 'for comprehension' on one line:
release_software_packages = [software_package for software_package in software_packages if over_30000(software_package)]
## Read this as "Give me the software package for each software package in the software packages list, but only if the software package is over 30000".
## For-comprehensions are tricky stuff and we can usually use a for loop instead, so don't get too bogged down in remembering this.
# Modules
## Programming languages don't include everything as standard - There'd be too much going on!
## If we want to use file system operations, we need to import a module to do it.
import os # This says: For the rest of this program or script, make the 'os' module available
current_working_directory = os.getcwd()
# Two things to notice here:
## The . between os and getcwd() tells us that getcwd() belongs to the 'os' module
## The brackets after getcwd are for an empty set of parameters. 'getcwd' is a function!
print(current_working_directory)
# Challenge 2: Print a list of the files and folders in the current directory.
## The online documentation for the os module includes this information on listdir:
## os.listdir(path)
## Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order.
## It does not include the special entries '.' and '..' even if they are present in the directory.
# Sonal & Marc
listdir = os.listdir(current_working_directory)
for list in listdir:
print(list)
## The docs for most modules can be found by Googling for them and the word 'python'. Here's the documentation for the os module:
## https://docs.python.org/2/library/os.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment