Update a bit for Python 3 course.

This commit is contained in:
Josh Mudge 2018-12-15 16:25:43 -07:00
parent fc64fd9ab1
commit 07ecd3d237
1 changed files with 88 additions and 7 deletions

View File

@ -10,13 +10,13 @@ It's gratis and accepts Python 3 syntax.
https://discuss.codecademy.com/t/what-does-it-mean-that-python-is-an-object-oriented-language/297314 https://discuss.codecademy.com/t/what-does-it-mean-that-python-is-an-object-oriented-language/297314
# Errors (ex5) # Errors (ex6, CH1, P3)
"Mismatched quotes will cause a SyntaxError' "SyntaxError means there is something wrong with the way your program is written — punctuation that does not belong, a command where it is not expected, or a missing parenthesis can all trigger a SyntaxError.
"SyntaxError: EOL while scanning a string literal" A NameError occurs when the Python interpreter sees a word it does not recognize. Code that contains something that looks like a variable but was never defined will throw a NameError."
No quotes will cause a NameError (interprets strings as commands. ) SyntaxError example: `SyntaxError: EOL while scanning string literal`
# Math (ex6) # Math (ex6)
@ -26,6 +26,8 @@ amazing_subtraction = 981 - 312
trippy_multiplication = 38 * 902 trippy_multiplication = 38 * 902
happy_division = 540 / 45 happy_division = 540 / 45
sassy_combinations = 129 * 1345 + 120 / 6 - 12 sassy_combinations = 129 * 1345 + 120 / 6 - 12
exponents = (16 ** 0.5) # 16 to the 1/2th power. (4)
remainder = (15 % 2) # The remainder (and thus the result) equals 1
``` ```
## Find the remainder of a number using % ## Find the remainder of a number using %
@ -57,19 +59,87 @@ Are indicated by # or """This is not for running"""
An integer is like `5`, a float is a number with a decimal point like `5.0`. They can also be in scientific notation like `2.3e7` An integer is like `5`, a float is a number with a decimal point like `5.0`. They can also be in scientific notation like `2.3e7`
To make sure math like `7/2` = `3.5` is correct is by inputting it into Python like `7./2.` or `float(7)/2` In Python 2, you need to make sure math like `7/2` = `3.5` is correct is by inputting it into Python like `7./2.` or `float(7)/2`
## Limitations of floats
Floats are limited by the number of digits. For example `1/3 = 0.3`
```
>>> format(math.pi, '.12g') # give 12 significant digits
'3.14159265359'
>>> format(math.pi, '.2f') # give 2 digits after the point
'3.14'
```
# Strings # Strings
Multi-line strings are marked by ```"""Mulit- Multi-line strings are marked by
```"""
Mulit-
line line
strings"""``` strings"""
```
# Booleans (True/False) # Booleans (True/False)
True = int(1) True = int(1)
False = int(0) False = int(0)
# Datatypes
Force treating as a string: str(7)
Force treating as an integer: int("7")
Froce treating as a float: float(7)
# Escaping Characters
Simply add a `\` to escape a character that would otherwise cause issues.
# Arrays / Indexes
`cows = "cows"[0]`
This sets the variable `cows` to the 0th letter of the string `"cows"` which is `c`. These indexes start at 0, not 1.
# Strings
## String Methods
`len(var)` Get length of string.
`var.lower()` Force lowercase
`var.upper()` Force uppercase
`str(var)` Force treating variable as a string.
If it uses dot notation like `.lower()`, it works exclusively on strings.
## Concatenation
`"Ten times a cow is equal to " + result + " with 10 times as many breeding opportunities."`
## String Formatting with %
`"%s %s - 2020" % (month, day) # Replace %s with a variable. First the month, then the day.`
`Add %03d to specify a signed integer padded 2 places with zeros. For example, 2 becomes 02.`
This is super useful for displaying dates like this: `print("%02d-%02d-%02d") % (now.month, now.day, now.year)` or time like this: `print '%02d:%02d:%04d' % (now.hour, now.minute, now.second)` (Ch3, Ex. 4)
## Date and Time (Ch3)
Grab the current time:
```
from datetime import datetime
now = datetime.now()
year = now.year
month = now.month
day = now.day
```
# Fun Projects # Fun Projects
Design a shop using Ex7 and Ex9 as a frame: Design a shop using Ex7 and Ex9 as a frame:
@ -92,3 +162,14 @@ price_per_cucumber = 3.25
total_cost = cucumbers * price_per_cucumber total_cost = cucumbers * price_per_cucumber
print(total_cost) print(total_cost)
``` ```
Cool concept from Ch2 Ex15:
```
name = raw_input("What is your name? ")
quest = raw_input("What is your quest? ")
color = raw_input("What is your favorite color? ")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)
```