diff --git a/notebooks/modules-and-packages.ipynb b/notebooks/modules-and-packages.ipynb index 6ce0d31..3a04da1 100644 --- a/notebooks/modules-and-packages.ipynb +++ b/notebooks/modules-and-packages.ipynb @@ -104,7 +104,7 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 3.0 }, "file_extension": ".py", "mimetype": "text/x-python", @@ -116,4 +116,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/slides/1-introduction.html b/slides/01-introduction.html similarity index 93% rename from slides/1-introduction.html rename to slides/01-introduction.html index a9a64c5..35709ed 100644 --- a/slides/1-introduction.html +++ b/slides/01-introduction.html @@ -39,7 +39,7 @@
-
@@ -115,6 +115,19 @@ background: #3F3F3F; } + .reveal li pre { + width: 100%; + } + + .reveal h6 { + color: #558D85; + font-weight: 300; + text-align: right; +} + + + + + + + + + + +
+
+
+
+
+
+ + + + + + + + + + \ No newline at end of file diff --git a/slides/content/1-introduction.md b/slides/content/01-introduction.md similarity index 85% rename from slides/content/1-introduction.md rename to slides/content/01-introduction.md index 80f365a..20c6842 100644 --- a/slides/content/1-introduction.md +++ b/slides/content/01-introduction.md @@ -5,11 +5,6 @@ --- -# Py...what? - - --- - ### About * Guido Van-Rossum side project in the late 1980's @@ -134,6 +129,21 @@ $ python /tmp/example.py hello world! +-- +### IDE - PyCharm + +[PyCharm](https://www.jetbrains.com/pycharm/) is a great python IDE. +A commercial and a community versions are available for download. + +Here are some tips for PyCharm: + +- You can set up both python 2 and python 3 side by side. +- Use `Alt+Enter` for quick fixes, including *auto import* (which is very useful). +- Use `Ctrl+Shift+A` to find any command or setting! +- Use `Ctrl+Alt+L` to reformat your code. +- Use `Ctrl+Shift+F10` to run the current file. +- Use `Shift+F10` to run again the last file. +- Use `Ctrl+Space` for method/variable autocomplete. --- diff --git a/slides/content/2-variables-and-data-types.md b/slides/content/02-variables-and-data-types.md similarity index 74% rename from slides/content/2-variables-and-data-types.md rename to slides/content/02-variables-and-data-types.md index f04ac01..e16230c 100644 --- a/slides/content/2-variables-and-data-types.md +++ b/slides/content/02-variables-and-data-types.md @@ -157,7 +157,7 @@ And what about `not 3`? The following string literals are equivalent: "Hello World!" - 'Hello World!" + 'Hello World!' """Hello World!""" '''Hello World!''' @@ -204,19 +204,19 @@ in Python docs for more information. To disable escape sequences, raw string literals can be used: - >>> print "c:\windows\newstuff\todo" # OOPS! + >>> print('c:\windows\newstuff\todo') # OOPS! c:\windows ewstuff odo - >>> print r"c:\windows\newstuff\todo" # Better. + >>> print(r'c:\windows\newstuff\todo') # Better. c:\windows\newstuff\todo -- Sadly, raw string literals cannot end with a backslash: - >>> print r"c:\work\" + >>> print(r'c:\work\') SyntaxError:... - >>> print r"c:\work" + "\\" # workaround. + >>> print(r'c:\work' + '\\') # workaround. c:\work\ --- @@ -271,7 +271,6 @@ All the methods bellow return new string (there is no in place operations!). 'hello, and ,welcome'.split(',', maxsplit=1) # -> ['hello', ' and ,welcome'] - * splitlines - splits and leaves the `\n` out '''hi @@ -287,4 +286,72 @@ All the methods bellow return new string (there is no in place operations!). * strip, rstrip, lstrip - removes spaces and new lines from the ends of a string: - " hello! \n".strip() # -> "hello!" \ No newline at end of file + ' hello! \n'.strip() # -> 'hello!' + +--- + +# String formatting + +-- +### Basic formating + + # getting user input + name = input() # python3 + name = raw_input() # python2 + + # old style formatting + print('Hello %s!' % name) + + # new style formatting + print('Hello {}!'.format(name)) + +-- +### More formating +- Named formatting + + >>> '{name} {age}'.format(age=5, name='Mike') + mike 5 + +- `{:4}{:7}` at least x number of chars +- `{:b}{:x}` formats number as binary or hex +- in short,`{name!conversion:format}` provides options on top of `{}` + +-- +### Resources +- [Format Specification Mini-Language](https://docs.python.org/2/library/string.html#format-specification-mini-language). +- [Examples](https://docs.python.org/2/library/string.html#format-examples) +- [String Formatting Cookbook](http://ebeab.com/2012/10/10/python-string-format/) +- [Common use cases](http://pyformat.info/) + + +-- +###### Excercise 1 + +Complete the following code: + + name = 'joe' + sons = 2 + daughters = 3 + + # ----- ENTER YOUR CODE HERE -------- + s = '' + # ----------------------------------- + + print(s) + assert s == 'Joe has 5 kids.' + +-- +###### Excercise 2 + +Use the code from the previous exercise to write a short program that asks + the user for input and prints the answer as following: + + What is the parent name? + > joe + How many sons does he have? + > 2 + How many daughters does he have? + > 3 + Joe has 5 kids. + + diff --git a/slides/content/03-control-structures.md b/slides/content/03-control-structures.md new file mode 100644 index 0000000..9d8d357 --- /dev/null +++ b/slides/content/03-control-structures.md @@ -0,0 +1,262 @@ + +# Control structures + +### Pycubator + +--- + +# If and While + +-- +### Simple while + + while True: + print("Enter your name (or nothing to exit)") + name = input() # python2: raw_input() + if not name: + print("Bye!") + break + print("Hello {}!".format(name)) + +-- +### Break and continue: + + SECRET = "xyzzy" + + while True: + password = input("Please enter your password: ") + if not password: + continue + if password == SECRET: + break + + print "Wrong password!" + + + print "Welcome!" + +-- +###### Exercise #1 +### Simple calc + +Ask the user to enter numbers. +Whenever the user enters an empty answer, show the sum of all entered numbers and quit. + + > 10 + > 45 + > 20 + > 90 + > + Result: 165 + +-- +###### Excersice #2 +### Guessing game + +- Player 1 enters a secret number between 1 and 100 (player 2 looks aside). +- The screen is cleared. (hint: `'\n' * 100`) +- Player 2 tries to guess the number. +- According to the input the program prints: + - "Your guess is too high, try again!" + - "Your guess is too low, try again!" + - "Your guess is correct!" +- If the guess is correct, the program prints the number of attempts and the game is done. + +-- +###### Excersice #3 +### Human Vs. Machine! + +- We continue to develop the guessing game, but this time your computer has to guess your secret number. +- Choose a secret number between 1 and 100 , but don't tell it to the computer! +- The computer will try to guess: "is your number x?". If you answer with y the game ends + and the number of attempts is printed to screen. +- Otherwise, the computer asks: "Is my guess higher than the secret number?" + and you answer with y or n. +- The computer asks again until she finds your number! + + +-- +###### Excersice #4 +### Fibonacci + +Make a program that prints the [Fibonacci series][fibo] below 10,000: + + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + ... + +[fibo]: https://en.wikipedia.org/wiki/Fibonacci_number#List_of_Fibonacci_numbers + +--- + +# For loops + +-- +### For on a string + + >>> for c in "Hello World!": + print(c, end=' ') + H e l l o W o r l d ! + +-- +### Using range() + + >>> for i in range(10): + print(i, end=',') + 0,1,2,3,4,5,6,7,8,9, + + >>> for i in range(1, 11, 2): + print(i, end=',') + 1,3,5,7,9, + + >>> for i in range(10, 0, -1): + print(i, end=',') + 10,9,8,7,6,5,4,3,2,1, + +-- +### Range defenition + +* `range(n)` produces `[0, 1, ..., n-1]` +* `range(i, j)` produces `[i, i+1, ..., j-1]` +* `range(i, j, k)` produces `[i, i+k, ..., m]` + +-- +### Enumarate + >>> for i, c in enumerate("Hello World!"): + print(i, c) + 0 H + 1 e + 2 l + 3 l + 4 o + 5 + 6 W + 7 o + 8 r + 9 l + 10 d + 11 ! + +-- +###### Exercise #5 +### Simple Square + +Input a number (`n`), and print a square of `n*n` asterisks. +Input - an integer between 1 and 50. Sample Input: + + 3 + +Sample Output: + + *** + *** + *** + +-- +###### Exercise #6 +### Pyramid + +Build a Pyramid! + +Input - An integer between 1 and 20 Sample. Sample input: + + 5 + +Sample Output: + + * + *** + ***** + ******* + ********* + +-- +Sample Input: + + 1 + +Sample Output: + + * + +--- + +# Nested loops + +-- +### Nested loops + + for i in range(10): + for j in range(10): + print i, j + +-- +###### Excercise #7 +### Multiplication Table + +Generate a multiplication table. + +Input: An integer between 1 and 50 + +Sample Input: + + 5 + +Sample Output: + + 1 2 3 4 5 + 2 4 6 8 10 + 3 6 9 12 15 + 4 8 12 16 20 + 5 10 15 20 25 + +--- + +# Functions + +-- +### Defenition + + >>> def increment(x): + return x + 1 + >>> increment(3) + 4 + +* Colon (:) indicates start of a block +* Following lines are indented +* Function declaration doesnt specify return type +* all functions return a value (None if not specified) +* Parameter data types are not specified either + +-- +###### Excercise #8 +### Rotate a word + +Create a function that rotates a word by 1 character to the right: + + def rotate(s): + # ---- YOUR CODE HERE --- + return "something" + # ----------------------- + + assert "dabc" == rotate("abcd") + assert "hello world" == rotate("ello worldh") + assert "x" == rotate("x") + +-- +###### Excercise #9 +### Nachmanize + +Create a function that returns a string nachmanized: + + assert nachmannize("abcd") == "a ab abc abcd" + diff --git a/slides/content/compound-data-types.md.draft b/slides/content/04-compound-data-types.md.draft similarity index 76% rename from slides/content/compound-data-types.md.draft rename to slides/content/04-compound-data-types.md.draft index bb9e08c..7535934 100644 --- a/slides/content/compound-data-types.md.draft +++ b/slides/content/04-compound-data-types.md.draft @@ -1,25 +1,20 @@ -# Compound Data Types +# Compound data types -- ### Overview -- Data types that are compound of smaller pieces, example: a string is made up from smaller strings containg a single character. -- Support similar methods, although each type has it's own unique qualities and will be effective in certain use cases (and not others). +- Data types that are compound of smaller pieces, example: a string is made up from smaller strings + containing a single character. +- Support similar methods, although each type has it's own unique qualities and will be effective in + certain use cases (and not others). - Types included are: string, tuple, list, dictionary, set. --- -# String +# More with Strings! --- -## Basics -- Holds a string of charcters. -- `'x'` or `"x"` for single lines. -- `'''x'''` for multiple lines -- Immutable - -- ## Slicing - Index with square brackets: `s[1]` @@ -27,47 +22,15 @@ - Slices: - Basic syntax is `s[start:end]`: `s[1:4]` - Slicing from the beginning of the string: `s[:5]` - - Slicing untill the end of the string: `s[3:]` + - Slicing until the end of the string: `s[3:]` - Negative indexes cam also be used in slices: `s[-3:-1]` --- -## Methods -- `s.split(sep)`: returns a list of substrings separated by sep -- `s.strip()`: strips whitespace from ends -- `s.strip('abc')`: specify non-whitespace chars to remove (each separtly) -- `s.isspace()`: returns True if all chars in s are whitespace -- `s.lower()`: converts all characters to lowercase - --- -## Join -- `s.join(str_list)`: concatenates the strings in `str_list` with s as a separator. -- When s is empty string: efficient way to concatenate strings -- Use space as s to join words with spaces - --- -## Find and replace -- `s.find(sub)`: finds the starting index of the first occurrence of sub in s -- `s.replace(old,new)`: replaces all occurrences of old in s with new - --- -## Formating (1) -- `s.format(arg1,arg2)`: replaces `{}` in s with args -- `{name!conversion:format}` provides options on top of `{}` -- Use `{0}{1}...{n}` to refer to positional arguments -- Use `{name}` and then `s.format(name=arg)` for named args -- `{!s} {!r} {!a}` call `str()`, `repr()` or `ascii()` before substitution -- `{:4}{:7}` at least x number of chars -- `{:b}{:x}` formats number as binary or hex - - --- -## formating (2) -- Lots of other stuff in [Format Specification Mini-Language](https://docs.python.org/2/library/string.html#format-specification-mini-language). -- [More examples](https://docs.python.org/2/library/string.html#format-examples) --- + # Tuple + -- ## Overview - Tuples are used for grouping ordered data diff --git a/slides/content/data-types-old.md.draft b/slides/content/data-types-old.md.draft index 238f265..60c4c60 100644 --- a/slides/content/data-types-old.md.draft +++ b/slides/content/data-types-old.md.draft @@ -84,7 +84,9 @@ Super cool: ### Ranges -* range(n) produces [0, 1, ..., n-1]* range(i, j) produces [i, i+1, ..., j-1]* range(i, j, k) produces [i, i+k, ..., m] +* range(n) produces [0, 1, ..., n-1] +* range(i, j) produces [i, i+1, ..., j-1] +* range(i, j, k) produces [i, i+k, ..., m] >>> range(5, 25, 3) [5, 8, 11, 14, 17, 20, 23] diff --git a/slides/index.html b/slides/index.html index c6ee775..3577e19 100644 --- a/slides/index.html +++ b/slides/index.html @@ -20,9 +20,11 @@

Pycubator

    -
  1. Introduction
  2. +
  3. Introduction
  4. -
  5. Variables and data types
  6. +
  7. Variables and data types
  8. + +
  9. Control structures