Summary of Python 3 Tutorial by Net Ninja (Part 1 of 3)

JL’s Self-teaching Story #6

Lim
Lim
Sep 7, 2018 · 7 min read
Copyright © 2018 Python

[JL’s Self-teaching Story] Series

[Net Ninja] JS Regular Expressions
[Net Ninja] Vue JS2 (Part1, Part2, Part3)
[Net Ninja] Vuex
[Net Ninja] Python3 (Part1(current), Part2, Part3)
[Net Ninja] Django (Part1, Part2, Part3)
[Net Ninja] Sass (Part1, Part2)
[Sean Larkin] Webpack4 (Part1, Part2, Part3, Part4)

🌲 This is the first part of my summary of Python 3 Tutorial by Net Ninja on YouTube.


1) Installing Python 3
2) Numbers
3) Strings
4) Lists
5) Standard Input
6) String Formatting
7) If Statements
8) For Loops
9) While Loops
10) Ranges

1) Installing Python 3 (YouTube)

On MacOS, you don’t need to install python, unless you’d like to update it to the latest version. To check version of python on your computer, type python --version on the terminal for MacOS and the command line for Windows.


*** The following information is not covered in the tutorial video.

I read “Why you need Python environments and how to manage them with Conda . Gergely Szerovay explained a few reasons of why he recommended to have a different Python environment for each project. The reasons don’t apply to my situation, since I’m just following a tutorial. But, I decided to set up an environment for this tutorial to practice for a future project.

Gergely said, “The two most popular tools for setting up environments are:

  • PIP (a Python package manager; funnily enough, it stands for “Pip Installs Packages”) with virtualenv (a tool for creating isolated environments)
  • Conda (a package and environment manager)”

In the article, he covered how to use Conda and mentioned that he preferred it because of clear structure, transparent file management, flexibility, and multipurpose. Among different installers, he recommended Miniconda.

As a Mac user, I followed steps on a GitHub page. After that, I typed the following code in the terminal(command line for Windows) to create a new environment:

conda create --name <name of the new environment> python=<python version number>(e.g., conda create --name nn_python3 python=3.7.0)
  • Activate: source activate <name of the environment>
  • Deactivate: source deactivate

After activating the environment, the environment name is shown in the terminal in the round brackets like the following:

(nn_python3) Hyejungs-MacBook-Air:python-3-playlist HyejungLim$

You can see the list of all virtual environments in your computer with conda env list. In my case, it’s the following:

# conda environments:
#
base * /Users/HyejungLim/miniconda3
nn_python3 /Users/HyejungLim/miniconda3/envs/nn_python3

After going through all the process, I was able to activate the new environment. But, I got the following error message : pyenv: no such command ‘sh-activate’.

I googled it and found the following answer on the pyenv GitHub repository : “The sh-activate command is part of the pyenv-virtualenv plugin”.

So, I installed pyenv-virtualenv. But, I wasn’t even able to activate the environment and received another error message, which is "pyenv-virtualenv: version `nn_python3' is not a virtualenv”.

I solved the issue by uninstalling a miniconda, reinstalling it, and repeating the process of creating a new environment by using conda.

I ran into the problem of pyenv-virtualenv: version `nn_python3' is not a virtualenv”.

This time, I followed the instruction on here.

source ~/anaconda3/etc/profile.d/conda.sh
conda activate <name of the environment>

Someone commented as “That seems a messy solution to the issue. Now there is a source line in your bash scripts that has to explicitly reference your conda installation path, making them not very portable.

>>> Unfortunately, I cannot understand what that means with my current knowledge. Well… Running the code above solve my issue for now.


2) Numbers (YouTube)

To use Python in the terminal or command line, type python.

The interactive shell is expressed as >>>.

To exit the interactive shell, enter ctrl + Z.

>>> type(5)   
<class 'int'>
>>> type(3.142)
<class 'float'>
>>> 5 / 5
1.0 (a float is always returned when division is used)
>>> 5 // 5
1 (In order to get an integer back when division is used, we should use '//')
>>> 5 ** 3
125 (5 to the power 3)
>>> 10 % 3
1 (remainder)
>>> (5 + 5) * 2
20 (changed the operation order by wrapping '5+5' with parentheses)

3) Strings (YouTube)

>>> 'I'm going to a bookstore.'
SyntaxError: invalid syntax (can't use a single quote inside of single quotes)
>>> 'I\'m going to a bookstore.'
"I'm going to a bookstore." (we can escape a single quote with a backslash)
[slice]
>>> greet = 'howdy'
>>> greet[0]
'h' (first letter from the beginning)
>>> greet[-1]
'y' (first letter from the end)
>>> greet[0:4]
'howd' (slice(:) the string, including the letter with index '0' and NOT including the letter with index '4')
>>> greet
'howdy' ('slice' doesn't alter the original value)
[concatenation]
>>> str2 = 'wassup'
>>> greet + ', ' + str2
'howdy, wassup'
[multiplication]
>>> greet * 3
'howdyhowdyhowdy'
[uppercase]
>>> greet.upper()
'HOWDY'
>>> greet
'howdy' (the original value is not affected)
[split]
>>> colors = "red, black, blue"
>>> colors.split(',')
['red', 'black', 'blue']
>>> colors
'red, black, blue' (the original value is not affected)
[length]
>>> len(greet)
5

4) Lists (YouTube)

[concatenation]
>>> fib1 = [1, 1, 2, 3, 5, 8, 13]
>>> fib2 = [21, 34, 55]
>>> fib1 + fib2
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> fib = fib1 + fib2
[Modify a list: 'append', 'pop', 'remove']
>>> fib.append(89)
>>> fib
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] ('append' added a number at the end of the list)
>>> fib.pop()
89
>>> fib
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] ('pop' removed a number at the end of the list)
>>> fib.append(55)
>>> fib
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 55]
>>> fib.remove(55)
>>> fib
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] ('remove' removed only the first instance of the repeated number)
>>> del(fib[9])
>>> fib
[1, 1, 2, 3, 5, 8, 13, 21, 34] ('del(fib[9])' removed a number of the index '9' in the 'fib' list)
[A list within a list]
>>> nums = [fib1, ['a','b','c']]
>>> nums
[[1, 1, 2, 3, 5, 8, 13], ['a','b','c']]
>>> nums[1][2]
'c'

5) Standard Input (YouTube)

  • To comment in a Python file, type # .
  • To comment multiple lines, drag the lines and press / + cmd(for Mac) or ctrl(for Window) .
  • To run a Python file in the terminal(command line for Windows): python <file name>.py.
# Let’s calculate an area of a circle.[projects > area_calc.py]
radius = input('Enter the radius of your circle (meters): ')
area = 3.142 * radius**2
print('The area of your cicle is: ', area)
>>> python area_calc.py

We get an error. Because the computer accepts ‘10’ as a string, not a number. So, we need to change radius**2 to int(radius**2).


6) String Formatting (YouTube)

[lessons > string_format.py]
num1 = 3.123456789
num2 = 10.123456
# format method
print('num2 is {1} and num1 is {0}'.format(num1, num2))
print('num1 is {0:.3} and num2 is {1:.3f}'.format(num1, num2))
# using f-strings
print(f'num1 is {num1:.3f} and num2 is {num2:.3}')
-----------------------------------
>>> python string_format.py
num2 is 10.123456 and num1 is 3.123456789
num1 is 3.12 and num2 is 10.123
num1 is 3.123 and num2 is 10.1
  • Inside curly brackets, enter an index of variables included in the “.format()
  • To format the string, put a colon(:), a dot(.), and a number(precision of a number) inside of the curly brackets. *** If we put “f” next to the number, we assign the scale of a number (instead of precision of a number).

Precision: the number of digits in a number

Scale: the number of digits the right of the decimal point in a number

  • Use “.format(the list of variables)” at the end.
  • Put “f” before the quotes
  • Inside curly brackets, enter the name of a variable instead.

7) If Statements (YouTube)

[lessons > if_elif.py]
age = int(input('Enter your age: '))
if age < 10:
print('good morning')
elif age < 40:
print('good afternoon')
else:
print('good evening')

Conditional statements are expressed with if <condition1>:, elif <condition2>:, and else:.

> You can have as many as elif.


8) For Loops (YouTube)

[lessons > loops.py]
colors = ['red', 'yellow', 'green', 'black']
for color in colors[1:3]:
print(color)
-----------------------------------
>>> python loops.py
yellow
green

> colors[1:3]

  • Start : including index 1
  • End: excluding index 3
for color in colors:
if color == 'yellow':
print(f'{color}: banana')
break
else:
print(color)
-----------------------------------
>>> python loops.py
red
yellow: banana

> formatted the string with f-string.

> break stops the loop.


9) While Loops (YouTube)

[lessons > loops.py]
age = 10
num = 0
while num < age:
if num == 0:
num += 1
continue
if num % 2 == 0:
print(num)
if num > 4:
break
num +=1
-----------------------------------
>>> python loops.py
2
4

> We can skip “0” by adding “1” & continue through the loop when the loop reaches “0”. (We get caught in an infinite loop if we don’t add “1”.)


10) Ranges (YouTube)

[lessons > ranges.py]
for n in range(3):
print(n)
-----------------------------------
>>> python ranges.py
0
1
2

> range(3)

  • Start from “0” & end before “3
  • Count by “1” (default)
[lessons > ranges.py]
for n in range(3,6):
print(n)
-----------------------------------
>>> python ranges.py
3
4
5

> range(3,6)

  • Start from “3” and end before “6
  • Count by “1” (default)
[lessons > ranges.py]
for n in range(0,10,3):
print(n)
-----------------------------------
>>> python ranges.py
0
3
6
9

> range(0,10,3)

  • Start from “3” & end before “6
  • Count by “3
[lessons > ranges.py]
colors = ['red', 'yellow', 'green', 'black']
for n in range(len(colors)):
print(n, colors[n])
-----------------------------------
>>> python ranges.py
0 red
1 yellow
2 green
3 black

> len(colors)

  • The length of “colors”, which is “4

> range(len(colors))

  • Start from “0” & end before “4
  • Count by “1” (default)
[lessons > ranges.py]
colors = ['red', 'yellow', 'green', 'black']
for n in range(len(colors) -1, -1, -1):
print(n, colors[n])
-----------------------------------
>>> python ranges.py
3 black
2 green
1 yellow
0 red

> range(len(colors) -1, -1, -1)

  • len(colors) -1: pstart from “3
  • -1: end before “-1” (to include “0” / colors[0] = ‘red’)
  • -1: count by “-1” (count backwards!)

If you’d like to read the next parts of my summary of Python 3 Tutorial by Net Ninja, please visit here for part 2 & here for part 3.


Thanks for reading! 💕 If you like this blog post, please clap👏

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade