Mathew K Analytics

Lesson 1 · Full-Length Courses

Python for Beginners: A Practical Introduction

This lesson introduces core Python programming concepts using a Jupyter Notebook inside VS Code. You will learn variables, user input, type conversion,…

⬇ 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 for Beginners: A Practical Introduction#

  • This lesson introduces core Python programming concepts using a Jupyter Notebook inside VS Code.
  • You will learn variables, user input, type conversion, strings, arithmetic and comparison operators, if statements, loops, lists, and tuples.
  • By the end, you will be able to write small, working Python programs and understand the errors Python shows you along the way.

What Can You Do With Python?#

  • Python is a multi-purpose programming language used across many fields.
  • It is the leading language for machine learning and data science projects.
  • With the Django framework, Python powers major websites such as YouTube, Instagram, Spotify, Dropbox, and Pinterest.
  • Python is also widely used for automation, saving time by handling repetitive tasks.

Installing Python#

  • Head over to python.org and go to Downloads to get the latest version of Python for your operating system.
  • Run the installer. On Windows, make sure to check the box that says Add python.exe to PATH before clicking Install, since this lets your terminal and VS Code find Python.
  • On macOS, simply run the installer package and follow the prompts.

Setting Up VS Code and Your First Notebook#

  • Install Visual Studio Code from code.visualstudio.com.
  • Inside VS Code, 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, 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.
print('Hello World')
Hello World
age = 20
print(age)
20
age = 30
print(age)
30
price = 19.95
first_name = 'Alex'
is_online = True
print(price, first_name, is_online)
19.95 Alex True
is_online = false
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 is_online = false

NameError: name 'false' is not defined

Practice Exercise: Hospital Check-In#

  • Imagine you are writing a program for a hospital checking in a new patient named John Smith, who is 20 years old.
  • Declare variables to store the patient's name, age, and whether they are a new patient, then print them out.
name = input('What is your name? ')
print('Hello ' + name)
Hello Alex
birth_year = input('Enter your birth year: ')
age = 2024 - birth_year
print(age)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[7], line 2
      1 birth_year = input('Enter your birth year: ')
----> 2 age = 2024 - birth_year
      3 print(age)

TypeError: unsupported operand type(s) for -: 'int' and 'str'
birth_year = input('Enter your birth year: ')
age = 2024 - int(birth_year)
print(age)
34

Practice Exercise: Basic Calculator#

  • Write a program that asks the user to enter two numbers, one at a time, allowing whole numbers or decimals.
  • Print the sum of the two numbers, labeled clearly, such as sum is 30.
first = float(input('First number: '))
second = float(input('Second number: '))
sum = first + second
print('Sum is ' + str(sum))
Sum is 30.5
course = 'Python for Beginners'
print(course.upper())
print(course.lower())
print(course.find('for'))
print(course.replace('Beginners', 'Everyone'))
print('Python' in course)
PYTHON FOR BEGINNERS
python for beginners
7
Python for Everyone
True
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 // 3)
print(10 % 3)
print(10 ** 3)
13
7
30
3.3333333333333335
3
1
1000
x = 10
x = x + 3
print(x)
x += 3
print(x)
13
16
x = 10 + 3 * 2
print(x)
y = (10 + 3) * 2
print(y)
16
26
print(3 > 2)
print(3 >= 3)
print(3 < 2)
print(3 <= 2)
print(3 == 2)
print(3 != 2)
True
True
False
False
False
True
price = 25
print(price > 10 and price < 30)
price = 5
print(price > 10 or price < 30)
print(not (price > 10))
True
True
True
temperature = 35
if temperature > 30:
    print('It is a hot day')
    print('Drink plenty of water')
elif temperature > 20:
    print('It is a nice day')
elif temperature > 10:
    print('It is a bit cold')
else:
    print('It is a cold day')
print('Done')
It is a hot day
Drink plenty of water
Done

Practice Exercise: Weight Converter#

  • Write a program that asks for a weight, then asks whether that weight is in kilograms or pounds, entering k or l.
  • Convert and print the weight in the other unit. Pounds to kilograms multiplies by 0.45; kilograms to pounds divides by 0.45.
weight = input('Weight: ')
unit = input('(K)ilograms or (L)bs: ')
if unit.upper() == 'K':
    converted = float(weight) / 0.45
    print('Weight in pounds is ' + str(converted))
else:
    converted = float(weight) * 0.45
    print('Weight in kilograms is ' + str(converted))
Weight in kilograms is 76.5
i = 1
while i <= 5:
    print(i)
    i = i + 1
1
2
3
4
5
i = 1
while i <= 10:
    print(i * '*')
    i = i + 1
*
**
***
****
*****
******
*******
********
*********
**********

Lists#

  • Numbers, strings, and Booleans are Python's primitive, or basic, data types.
  • Python also has complex data types built from these, and lists are one of the most common.
  • We use a list whenever we want to represent a collection of objects, such as a list of names or numbers.
names = ['John', 'Bob', 'Alex', 'Sam', 'Mary']
print(names)
print(names[0])
print(names[-1])
print(names[-2])
names[0] = 'Jon'
print(names)
['John', 'Bob', 'Alex', 'Sam', 'Mary']
John
Mary
Sam
['Jon', 'Bob', 'Alex', 'Sam', 'Mary']
names = ['John', 'Bob', 'Alex', 'Sam', 'Mary']
print(names[0:3])
print(names)
['John', 'Bob', 'Alex']
['John', 'Bob', 'Alex', 'Sam', 'Mary']
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers)
numbers.insert(0, -1)
print(numbers)
numbers.remove(3)
print(numbers)
print(1 in numbers)
print(10 in numbers)
print(len(numbers))
[1, 2, 3, 4, 5, 6]
[-1, 1, 2, 3, 4, 5, 6]
[-1, 1, 2, 4, 5, 6]
True
False
6
numbers = [1, 2, 3, 4, 5]
for item in numbers:
    print(item)
1
2
3
4
5
for number in range(5):
    print(number)
for number in range(5, 10):
    print(number)
for number in range(5, 10, 2):
    print(number)
0
1
2
3
4
5
6
7
8
9
5
7
9
numbers = (1, 2, 2, 3)
print(numbers.count(2))
print(numbers.index(3))
2
3
numbers = (1, 2, 3)
numbers[0] = 10
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[26], line 2
      1 numbers = (1, 2, 3)
----> 2 numbers[0] = 10

TypeError: 'tuple' object does not support item assignment

Recap#

  • We covered variables, user input, and type conversion between strings, integers, floats, and Booleans.
  • We covered string methods, arithmetic, comparison, and logical operators, plus operator precedence.
  • We covered if, elif, and else for decision making, and while loops for repeating code.
  • We covered lists and tuples: creating them, indexing and slicing, adding and removing items, and iterating with for loops and range.
  • Practicing each exercise 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.