In [7]:
from notebook.services.config import ConfigManager
from IPython.paths import locate_profile
cm = ConfigManager(profile_dir=locate_profile(get_ipython().profile))
cm.update('livereveal', {
'theme': 'moon',
'transition': 'zoom',
'start_slideshow_at': 'selected',
})
Out[7]:
Unsure how your code should be written? PEP is a style guide for Python and provides details on what is expected.
snake_caseMyClassExample)Sometimes, you need to describe your code and the logic may be a bit complicated, or it took you a while to figure it out and you want to make a note.
You can't just write some text in the file or you will get errors, this is where comments come in! Comments are descriptions that the Python interpreter ignores.
Just type a # amd what ever you want to write and voíla!
It is ALWAYS a good idea to comment your code!
In [2]:
# Does this make sense without comments?
with open('myfile.csv', 'rb') as opened_csv:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print (', '.join(row))
In [2]:
# How about this?
#open csv file in readable format
with open('myfile.csv', 'rbU') as opened_csv:
# read opened csv file with spaces as delimiters
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
# loop through and print each line
for row in spamreader:
print (', '.join(row))
In [2]:
"Gavin" #String Literal
4 #Integer Literal
Out[2]:
3.14 is a float literal
In [ ]:
# declared and instantiated
name = "Gavin"
# declared, but not instantiated
new_name = None
In [1]:
x # does not exist so cannot print it
In [ ]:
x = 1
print(x)
In many languages, when a variable is instantiated, it is reserved into a block of memory and the variable points to that memory address, often unique for that variable.
In Python, it is more like that memory address is tagged with the variable name. So, if we create three variables that have the same literal value, then they all point to the same memory address. Like so:
In [6]:
a = 1
b = 1
c = 1
print(id(a))
print(id(b))
In [1]:
a,b,c = 1,1,1
name, age, yob = "chris", 26, 1989
print(name, age, yob)
print(id(name), id(age), id(yob))
a,b,c = "Name",12,"6ft"
print(a,b,c) #NOTE: always balance the left and the right. 5 variables must have 5 values!
These are keywords reserved in Python, so do not name any of your variables after these! You will learn about what many of these do throughout this course.
| False | class | finally | is | return |
|---|---|---|---|---|
| continue | for | lambda | try | True |
| def | from | nonlocal | while | and |
| del | global | not | with | as |
| elif | if | or | yield | assert |
| else | import | pass | break | except |
| in | raise | None |
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/variables.html
http://foobarnbaz.com/2012/07/08/understanding-python-variables/
http://www.diveintopython.net/native_data_types/declaring_variables.html
Python is Strongly Typed - The Python interpreter keeps track of all the variables and their associated types.
AND
Python is Dynamically Typed - Variables can be reassigned from their types. A variable is simply a value bound to a name, the variable does not hold a type, only the value does.
In [6]:
print("Hello " + "World") #ok
print("hello" + 5) #strongly typed means this cannot happen!
name = "Chris"
name = "Pi"
"pi" + 6 #Strongly typed means no adding different types together!
name = 3.14 #dynamically typed means yes to changing the type of a variable!
In [1]:
type('am I your type?')
Out[1]:
In [ ]:
'single_quotes'
What do single quotes usually mean in most languages?
Strings wrapped in 'single quotes' are typically chars (single characters). Python does not have this type.
char yes = 'Y' //char (Python does not have this)
string no = "no" //string
What are chars used for? What does Python have instead?
Chars are typically used as single character flags, like a 'Y' or an 'N' as an answer to a question, or to hold an initial.
Anything text based can be stored in a string but flags can be represented as a 0 or 1 or even using a Boolean value,, which is easiest to check against.
What happens if you use a single quote for strings and you write the word don't in the string? Try it out now!
How do we get around that?
When you want to include special characters (like ') then it is always good to escape them!
Ever seen a new line written as \n? That is an example of escaping.
An escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence.
This is pretty much always \
In [ ]:
'isn't # not gonna work
In [ ]:
'isn\'t' # works a charm!
If you do not want to escape every special character, maybe there is a better way?
"I am a string and I don't care what is written inside me"
"""I am a string with triple double quotes and I can
run across multiple lines"""
Double quotes is generally better as you do not have to escape these special characters.
The first one was a trick! Declaring a new variable means giving it no value!
The second one would be: age = 20
It would not be fun to have to create a new variable for every thing you want to store, right?
As well as being a huge inconvenience, it is actually really inefficient.
age = 40
# 1 year passes
age = 41
Easy, right? By using the same variable name, it is now associated with your new value and the old value will be cleared up.
In [5]:
print("Got something to say?")
print("Use the print statement")
print("to print a string literal")
print("float literal")
print(3.14)
more_string = "or variable"
print(more_string)
+ (add)- (subtract)/ (divide)* (multiply)There are more, but we will get to them!
In Python 3, // is floor division and / is floating point division
In [ ]:
float_type = 3.0
int_type = 5
print(int_type + float_type)
string_type = "hello"
print(string_type + float_type) # what is the error?
bool_type = True
print(string_type + bool_type)
print(int_type + bool_type) # does this work? Why?
In [ ]:
float_type = 3.0
int_type = 5
print(int_type * float_type)
string_type = "hello"
print(string_type * int_type)
bool_type = True
print(string_type * bool_type) #why does this work?
print(int_type * bool_type)
In [ ]:
year = year + 1
year += 1
str - converts an object to a string type len - prints the length of an object type - tells you the type of a literal or a variable int - converts a type to an integer
id - a unique id that relates to where the item is stored in memory isinstance - checks if an object if of the supplied type
In [6]:
dir("")
Out[6]:
In [5]:
help(int)
help("")
help(1)