Learn Python3 not Python2 Why?
- Quora Post - Free resources to learn Python
- /r/learnpython wiki Lots of resources here
- CodeAcademy
- CodeWars List of exercises and challenges to complete similiar to Hackerrank and Leetcode.com to practice
- InventWithPython books on automating, encryption, and games
- ThinkPython
- DiveIntoPython3
- Tiny Python Notebook table of notes and examples for Python syntax (pretty short)
- Fast.ai - Deep Learning for Coders Practical Deep Learning
- Deeplearning.ai
Learning and understanding the concepts are more important than the framework (i.e Pytorch or Tensorflow
- You should already have python3.6 installed(if you ran the brew setup guide below)
- Go to pytorch.org
- Select
- OS -> Mac OSX
- Package Manager -> pip
- Python -> 3.6 (you can verify the python version by running
python3 --version
in terminal) - CUDA -> doesn't matter, we aren't going to install GPU drivers yet
- Copy what's in the 'Run this command:' text box and paste into terminal
- After it's finished you should be done. To verify:
pip freeze | grep torch
should show you the version installed- run
import torch
print(torch.__version__)
- Install brew (PM marvin
!brew
for instructions) brew install python3
- Run and use
python3
in shell after you install to test syntax quickly - To install packages run
pip3 install <package>
- install better REPL
ptpython
use that instead ofpython3
interpreter to test code:pip3 install ptpython
then in terminal runptpython
- install better REPL
- Run Pyton online here
- Install virtualenv package
pip3 install virtualenv
- If you run
pip3 freeze
, you'll get a list of all pip packages installed and their versions globally.
- If you run
- Go to your project directory
cd <my_new_project_path>
- Run
python3.6 -m virtualenv venv
orvirtualenv venv
. It'll create a new folder to install your dependcies in. - There's also scripts created in the
venv
folder toactivate
a virtualenv - Let's activate it:
source venv/bin/activate
- You should notice that your terminal has changed.
(venv)
should be prepended to the front of the cmd line
- You should notice that your terminal has changed.
- Now when you install packages or run pip commands they'll be installed to and ran from the virtualenv
- If you do
pip freeze
, you'll notice it returns an empty list.- Try to install a pip package and rerun
pip freeze
- Try to install a pip package and rerun
- Now you can install packages normally with pip or install all packages found in a 'requirements.txt' with
pip -r requirements.txt
- When you run your scripts(e.g.
python3.6 do_stuff.py
it'll use packages found in env first) - To leave or
deactivate
the environment simply enterdeactivate
into the cmdline.
C/Python Differences (Stackoverflow Link)
C | Python |
---|---|
Procedural (list of instructions) | Multi (Object-oriented + Procedural + Functional) |
Static typed (int c = 10;) | Dynamic (c = 10) |
Compiled (Source -> Target language) Slow to develop, Fast to execute | Interpreted (Source -> Intermediate -> Target) Fast to develop, Slow to execute |
Whitespace insensitive | Whitespace sensitive |
Manual memory management | Automatic memory management |
Not easy to test | Very easy to test |
Fast | Slow |
- We can get to what matters faster with lesswork. We can simply go straight to experimentation and testing much more quickly. For most applications, you won't notice the "slowness" of Python.
- Most of the deep learning frameworks are written for or support Python.
// C - hello_world.c
#include <stdio.h>
int main(void)
{
char *s = "Hello, World!";
printf(s);
}
gcc hello_world.c -o hello_world.o
./hello_world.o
# Python - hello_world.py
s = "Hello, World" # Python has no declarations
print(s)
python hello_world.py
- You can add a
def main()
tohello_world.py
- note the commenting is different
#
for python to end of line//
to end of line or/* STUFF */
for multi-line comments
// C
#include <stdio.h>
void ft_nprint(const char *s_even, const char *s_odd, int c)
{
while (c--)
{
if (c % 2 == 0 && c % 3 == 0)
{
printf(s_even);
printf(s_odd);
}
else if (c % 2 == 0)
printf(s_even);
else if (c % 3 == 0)
printf(s_odd);
else
printf("NEVER");
}
}
#Python
def ft_nprint(s_even, s_odd, c):
while (c):
if (c % 2 == 0 and c % 3 == 0):
print(s_even)
print(s_odd)
elif (c % 2 == 0):
print(s_even)
elif (c % 3 == 0):
print(s_odd)
else:
print("NEVER")
c -= 1
- Whitespace matters in Python but you don't need brackets or semi-colons
// C
int sum(int a, int b)
{
return (a + b);
}
# Python
def sum(a, b):
return a + b
# Python
def main():
x = y = 0
a, b, c = 1, 2, 3 # a = 1, b = 2, c = 3
// C
int main(void)
{
int x, y;
int a, b, c;
x = y = 0;
a = 1;
b = 2;
c = 3;
}
# Python
i = 10
while (i):
print(i)
i -= 1
// C
int i = 10;
while (i--)
printf(i);
# Python
from math import sqrt
def main():
print("Sqrt of 10 is {}", sqrt(10));
// C
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("Sqrt of 10 is %d", sqrt(10));
}
# Python
nums = [1, 2, 3, 4, 5]
for i in range(0, len(nums)):
print(nums[i]) # iterate using an index
// C
int size = 5;
int nums[] = [1, 2, 3, 4, 5];
for (i=0; i < size; i++)
printf(nums[i]);
- You can't iterate over arrays using pointers in Python
- There aren't any pointers in Python
- Better to Google search this one