Mathew K Analytics

Lesson 4 · Full-Length Courses

Python Full Course for Beginners

This lesson walks through the fundamentals of Python: installing it, setting up a Jupyter Notebook in VS Code, variables and types, strings, numbers,…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Python Full Course for Beginners#

  • This lesson walks through the fundamentals of Python: installing it, setting up a Jupyter Notebook in VS Code, variables and types, strings, numbers, conditionals, loops, and functions.
  • Every example runs inside notebook cells, so you can experiment, tweak, and re-run each idea as you go.
  • No prior programming experience is required.

Installing Python#

  • Go to python.org, open Downloads, and get the latest version of Python for your operating system.
  • On Windows, before clicking Install, check the box that says 'Add python.exe to PATH'. This step saves you a lot of headaches later.
  • On macOS, simply run the installer package and follow the prompts.
  • To verify the install, open a terminal and type 'python --version' on Windows, or 'python3 --version' on macOS or Linux.

Setting Up VS Code and Your First Notebook#

  • Install Visual Studio Code from code.visualstudio.com.
  • Open the Extensions view and install the Python extension and the Jupyter extension, both published by Microsoft.
  • Open the Command Palette with Control plus Shift plus P (Command plus Shift plus P on Mac), type 'Create: New Jupyter Notebook', and press Enter.
  • In the top right corner of the notebook, click Select Kernel and choose your installed Python interpreter.
2 + 2
4
2 > 1
True
2 >
  Cell In[3], line 1
    2 >
       ^
SyntaxError: invalid syntax
print('Hello World')
print('*' * 10)
Hello World
**********

Linting and Formatting#

  • The Python extension in VS Code includes linting, which analyzes your code for potential errors as you type, before you even run a cell.
  • It also supports automatic code formatting according to PEP 8, Python's official style guide, so your code stays clean and consistent.
  • You can enable 'Format on Save' in VS Code's settings so your formatting is applied automatically.

Python the Language vs. Python the Implementation#

  • Python as a language is a specification: a set of rules and grammar for writing code.
  • CPython, the program we just installed from python.org, is the default implementation, written in C, that actually executes Python code.
  • Other implementations exist too, such as Jython (written in Java) and IronPython (written in C#), used to interoperate with Java or .NET code respectively.
  • When CPython runs a program, it first compiles the code into Python bytecode, then a virtual machine converts that bytecode into machine code and executes it.
students_count = 1000
print(students_count)
1000
rating = 4.99
is_published = True
print(rating, is_published)
4.99 True
is_published = false
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 is_published = false

NameError: name 'false' is not defined

Naming Variables Well#

  • Use descriptive, meaningful names, such as students_count instead of a mystical abbreviation like sc.
  • Use lowercase letters for variable names, and separate multiple words with an underscore (this style is called snake_case), since Python does not allow spaces in variable names.
  • Put a space around the assignment operator, the equals sign, as in x = 1, not x=1.
  • If you forget these conventions, saving the file with Format on Save enabled will often clean it up automatically, but it is still good practice to write clean code by hand.
course = 'Python Programming'
print(course)
message = """
Hi John,
This is Alex from the support team.
"""
print(message)
Python Programming

Hi John,
This is Alex from the support team.

course = 'Python Programming'
print(len(course))
print(course[0])
print(course[-1])
18
P
g
course = 'Python Programming'
print(course[0:3])
print(course[3:])
print(course[:3])
print(course[:])
Pyt
hon Programming
Pyt
Python Programming
course = 'Python Programming'
print('Python \"Programming\"')
print('Python\'s Programming')
print('Python \\ Programming')
print('Python \n Programming')
Python "Programming"
Python's Programming
Python \ Programming
Python 
 Programming
first = 'Alex'
last = 'Taylor'
full = first + ' ' + last
print(full)
full = f'{first} {last}'
print(full)
Alex Taylor
Alex Taylor
course = 'Python Programming'
print(course.upper())
print(course.lower())
print(course.title())
print('   Python   '.strip())
print(course.find('Pro'))
print(course.replace('P', 'J'))
print('Pro' in course)
print('Swift' not in course)
PYTHON PROGRAMMING
python programming
Python Programming
Python
7
Jython Jrogramming
True
True
print(10 / 3)
print(10 // 3)
print(10 % 3)
print(10 ** 3)
3.3333333333333335
3
1
1000
x = 10
x = x + 3
print(x)
x += 3
print(x)
13
16
import math
print(round(2.9))
print(abs(-2.9))
print(math.ceil(2.2))
3
2.9
3
x = input('Enter a number: ')
print(type(x))
y = x + 1
<class 'str'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[17], line 3
      1 x = input('Enter a number: ')
      2 print(type(x))
----> 3 y = x + 1

TypeError: can only concatenate str (not "int") to str
x = input('Enter a number: ')
x = int(x)
y = x + 1
print(f'x is {x} and y is {y}')
x is 1 and y is 2
print(bool(0))
print(bool(1))
print(bool(-1))
print(bool(''))
print(bool('False'))
False
True
True
False
True
print(10 > 3)
print(10 >= 3)
print(10 < 3)
print(10 <= 3)
print(10 == 10)
print(10 == '10')
print(10 != 3)
True
True
False
False
True
False
True
temperature = 35
if temperature > 30:
    print('It is warm')
    print('Drink plenty of water')
elif temperature > 20:
    print('It is nice')
else:
    print('It is cold')
print('Done')
It is warm
Drink plenty of water
Done
age = 22
message = 'Eligible' if age >= 18 else 'Not eligible'
print(message)
Eligible
high_income = True
good_credit = True
is_student = False
if high_income and good_credit:
    print('Eligible')
else:
    print('Not eligible')
if (high_income or good_credit) and not is_student:
    print('Eligible')
Eligible
Eligible
age = 22
if age >= 18 and age < 65:
    print('Eligible')
if 18 <= age < 65:
    print('Eligible')
Eligible
Eligible
for number in range(3):
    print(f'Attempt {number + 1}')
Attempt 1
Attempt 2
Attempt 3
for number in range(1, 4):
    print(number)
for number in range(1, 10, 2):
    print(number)
1
2
3
1
3
5
7
9
successful = True
for attempt in range(3):
    print('Sending...')
    if successful:
        print('Successfully sent!')
        break
else:
    print('Attempted 3 times and failed')
Sending...
Successfully sent!
for x in range(3):
    for y in range(2):
        print(f'({x}, {y})')
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)
print(type(range(5)))
for letter in 'abc':
    print(letter)
for item in [10, 20, 30]:
    print(item)
<class 'range'>
a
b
c
10
20
30
number = 100
while number > 0:
    print(number)
    number //= 2
100
50
25
12
6
3
1
while True:
    command = input('> ')
    if command.lower() == 'quit':
        break
    print(f'Echo: {command}')
Echo: 2 + 2
Echo: 3 * 2

Exercise: Even Numbers#

  • Write a program that prints the even numbers between 1 and 10, then prints a final message stating how many even numbers were found.
  • Hint: call range with 1 and 10, and check whether each number's remainder when divided by 2 equals 0.
count = 0
for number in range(1, 10):
    if number % 2 == 0:
        print(number)
        count += 1
print(f'We have {count} even numbers')
2
4
6
8
We have 4 even numbers
def greet():
    print('Hi there')
    print('Welcome aboard')

greet()
Hi there
Welcome aboard
def greet(first_name, last_name):
    print(f'Hi {first_name} {last_name}')


greet('Alex', 'Taylor')
greet('John', 'Smith')
Hi Alex Taylor
Hi John Smith
def greet(first_name, last_name):
    print(f'Hi {first_name} {last_name}')


greet('Alex')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[35], line 5
      1 def greet(first_name, last_name):
      2     print(f'Hi {first_name} {last_name}')
----> 5 greet('Alex')

TypeError: greet() missing 1 required positional argument: 'last_name'
def get_greeting(name):
    return f'Hi {name}'


message = get_greeting('Alex')
print(message)
Hi Alex
def greet(name):
    print(f'Hi {name}')


result = greet('Alex')
print(result)
Hi Alex
None
def increment(number, by=1):
    return number + by


print(increment(2))
print(increment(2, by=5))
3
7
def multiply(*numbers):
    total = 1
    for number in numbers:
        total *= number
    return total


print(multiply(2, 3))
print(multiply(2, 3, 4))
6
24

Recap#

  • We installed Python and set up a Jupyter Notebook in VS Code as our environment for writing and running code.
  • We covered variables, primitive types, strings and their methods, numbers and arithmetic, and type conversion with the input function.
  • We covered comparison and logical operators, if/elif/else, the ternary operator, and chained comparisons.
  • We covered for loops with range, break, nested loops, iterables, and while loops, including how to safely use an infinite loop.
  • We covered defining functions with required and optional parameters, returning values, keyword arguments, and variable-length arguments with an asterisk.
  • Practicing each example yourself in a Jupyter Notebook in VS Code is the best way to make these concepts stick.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.