Giter Site home page Giter Site logo

python-primer-jsong-staff's Introduction

Python Primer

This is a Jupyter Notebook

You can write Python code and it will execute. You can write the typical 'hello world' program like this:

print('hello world')

You can execute by pressing shift-enter. Try it! You can also click the Run button in the toolbar.

print('hello world')

You can do a lot more than just print "hello world"

This is a fully functioning Python3 interpreter so you can write functions and objects like in the next box.

Try printing the 21st Fibonacci number below instead of the 11th. You can add caching if you want to practice coding in Python.

def fib(n):
    if n in (0,1):
        return 1
    else:
        return fib(n-1) + fib(n-2)

print(fib(10))

Imports

You already have unit tests that are written for you. Your task is to make them pass. We can import various modules to make our experience using Jupyter more pleasant. This way, making everything work will be a lot easier.

# import everything and define a test runner function
from importlib import reload
from helper import run_test

import helper

A few things you should remember in Python3

Strings and bytes are now different

s = 'hello world'
b = b'hello world'

These may look the same but the 'b' prefix means that one is bytes. Basically, the on-disk characters on the system are bytes and the actual symbols in unicode are strings. A good explanation of the difference is here.

s = 'hello world'
b = b'hello world'

print(s==b) # False

# You convert from string to bytes this way:

hello_world_bytes = s.encode('ascii')
print(hello_world_bytes == b) # True

# You convert from bytes to string this way:

hello_world_string = b.decode('ascii')
print(hello_world_string == s) # True

Test Driven Exercise

Below is test driven exercise. The tests being run are in the test/index_test.py file. Again, if you want to see the code you can click on the Octocat icon on the tool bar. Your job on these Test Driven Exercises is to get the tests to pass.

In order to do this, you'll have to write the methods below. So go ahead and implement the bytes_to_str and str_to_bytes functions. Once you're done, run the blue Run Test button. Try it now!

(Getting Help)

If you can't get this, there is a solution.ipynb notebook on GitHub (click the Octocat GitHub button near the Run Test button to see the repository) with complete answers.

def bytes_to_str(b, encoding='ascii'):
    '''Returns a string version of the bytes'''
    pass


def str_to_bytes(s, encoding='ascii'):
    '''Returns a bytes version of the string'''
    pass

python-primer-jsong-staff's People

Watchers

James Cloos avatar  avatar

Forkers

seenevz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.