Python Flashcards
Why Python to learn as a beginner?
It’s syntax is relatively simple and you don’t have to worry about things like static syntax and compilation time
What is homebrew?
It is a package manager that helps us install software on our operating system
Who created Python?
Guido Van Rossum in 1991
What are some popular libraries?
NumPy, SciPy, BeautifulSoup, Scrapy, Twisted, Django
How does Python know how to run code?
Indentation…tab or four spaces…DO NOT MIX UP
How would you do Python for 1 to 255
def print1to255(): for i in range(1, 256): print i
Why version 2.7 and not 3.6?
Porting code over is easy, but going backwards is not so easy
What is a web server?
A computer system that processes HTTP requests
Why are companies using 2.7?
There are many reasons why this is the case, the primary being the expense of switching all of a company’s code base from one version to another
What is the Python shell?
The python shell is a command line interface we can use to interact with the Python interpreter. Once activated, we can type some Python code and see the results immediately.
If I divide 31/2 on the python shell command line, what is the result?
15, most programming languages treat integers (whole numbers) and floating point numbers, or floats (decimal numbers) differently. If you divid an integer by an integer, the result will always be an integer rounded down
Do you need to utilize var to declare a variable in Python?
No
What is a list in Python?
A list is an array
How can we comment out code in Python?
Using either # for single line, or triple quotes (double or single) for multiple lines UNLESS it’s the first line of code.
What are data types?
Data types refers to how the computer knows to classify information.
What are primitive data types? What are the most common?
Are the basic building blocks of a language.
Boolean values - Assess the truth value of something. It has only two values: True and False
Numbers - Integers (whole numbers), floating point numbers, and complex numbers
Strings - a text literal. Most pages in the web work with strings quite often
What are composite data types?
Collections composed of the above primitive types.
Tuples - A type of data that is immutable (can’t be modified after its creation) and can hold a group of values. Tuples can contain mixed data types.
Lists - A type of data that is mutable and can hold a group of values. Usually meant to store a collection of related data.
Dictionaries - A group of key-value pairs. Dictionary elements are indexed by unique keys which are used to access values.
Indentation, explain it’s significance
Python has no brackets, braces, or keywords to indicate the start and end of certain code blocks such as functions, if statements, and loops. The only delimited is the colon :
What is the equivalent of function in python?
def
How can you print a string with a variable?
print “my name is”, variable
print “my name is” + variable
What is string interpolation?
Utilizing a method to inject variables into your string.
first_name = “Zen”
last_name = “Coder”
print “My name is {} {}”.format(first_name,last_name)
What are the most commonly used string methods?
string. count(substring): returns number of occurrences of substring in string.
string. endswith(substring): returns a boolean based upon whether the last characters of string match substring.
string. find(substring): returns the index of the start of the first occurrence of substring within string.
string. isalnum(): returns boolean depending on whether the string’s length is > 0 and all characters are alphanumeric (letters and numbers only). Strings that include spaces and punctuation will return False for this method. Similar methods include .isalpha(), .isdigit(), .islower(), .isupper(), and so on. All return booleans.
string. join(list): returns a string that is all strings within our set (in this case a list) concatenated.
string. split(): returns a list of values where string is split at the given character. Without a parameter the default split is at every space.
How would you add information to a list?
.append()
What is the equivalent of array.slice(start, end not inclusive)?
x[1:4]
what is the similar function to javascript .length?
len(sequence)…can be applied to all sequences, including tuples and strings
What are some useful built-in functions for sequences?
Some built-in functions for sequences:
enumerate(sequence) used in a for loop context to return two-item-tuple for each item in the list indicating the index followed by the value at that index.
map(function, sequence) applies the function to every item in the sequence you pass in. Returns a list of the results.
min(sequence) returns the lowest value in a sequence.
sorted(sequence) returns a sorted sequence
What is a type?
int, boolean, string, list,
when creating a dictionary, indentation requirements are not as strict
for x in mydictionary: print x, mydictionary[x]
Dictionaries are good for more structure. Storing user information you can use a dictionary and it’s a lot easier
What is a set?
a = {1,2,3,4,5} and you can subtract a set from another set