Lesson 3 · Full-Length Courses
Learn Python: A Full Course for Beginners
This lesson covers Python from the ground up: variables, strings, numbers, lists, tuples, dictionaries, functions, conditionals, loops, files, modules, and…
- CourseFull-Length Courses
- Lesson3 of 7
- Video1 h 4 min
- FormatJupyter notebook · 43 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbLearn Python: A Full Course for Beginners#
- This lesson covers Python from the ground up: variables, strings, numbers, lists, tuples, dictionaries, functions, conditionals, loops, files, modules, and classes.
- Every example runs inside a Jupyter Notebook cell in VS Code, so you can run, tweak, and re-run each idea as you go.
- No prior programming experience is required.
Installing Python#
- Go to python.org, open Downloads, and download the latest version of Python 3 for your operating system.
- On Windows, before clicking Install, check the box that says 'Add python.exe to PATH', which lets your terminal and VS Code find Python.
- On macOS, run the installer package and follow the prompts.
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.
print('Hello World')
print(' /')
print('/ ')
print('| ')
print('|__')
character_name = 'John'
character_age = 35
print('There once was a man named ' + character_name + '. He was ' + str(character_age) + ' years old.')
character_name = 'Tom'
character_age = 50
print('There once was a man named ' + character_name + '. He was ' + str(character_age) + ' years old.')
character_name = 'John'
character_age = 35.5
is_male = True
print(character_name, character_age, is_male)
phrase = 'Learn Python'
print(phrase + '\n' + 'is fun')
print('He said, \'hello\'')
print('A backslash: \\')
phrase = 'Learn Python'
print(phrase + ' is cool')
print(phrase.lower())
print(phrase.upper())
print(phrase.upper().isupper())
print(len(phrase))
print(phrase[0])
print(phrase.index('P'))
print(phrase.replace('Learn', 'Master'))
print(3 + 4.5)
print(3 * 4 + 5)
print(3 * (4 + 5))
print(10 % 3)
import math
my_num = -5
print(abs(my_num))
print(pow(3, 2))
print(max(4, 6))
print(min(4, 6))
print(round(3.7))
print(math.floor(3.7))
print(math.ceil(3.2))
print(math.sqrt(36))
name = input('Enter your name: ')
age = input('Enter your age: ')
print(f'Hello {name}, you are {age}')
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
result = num1 + num2
print(result)
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
result = num1 + num2
print(result)
Mini Project: Mad Libs#
- A mad lib is a short story with a few words replaced by blanks, like a color, a plural noun, or a celebrity name, that the player fills in themselves.
- We will ask the user for a color, a plural noun, and a celebrity, then splice their answers into a short poem.
color = input('Enter a color: ')
plural_noun = input('Enter a plural noun: ')
celebrity = input('Enter a celebrity: ')
print('Roses are ' + color)
print(plural_noun + ' are blue')
print('I love ' + celebrity)
Lists#
- A list lets us store many related values together in one structure, rather than one value per variable.
- Lists can hold strings, numbers, Booleans, or a mix of all three.
friends = ['Kevin', 'Karen', 'Jim']
print(friends)
print(friends[0])
print(friends[2])
print(friends[-1])
friends[1] = 'Mike'
print(friends)
friends = ['Kevin', 'Karen', 'Jim', 'Oscar', 'Toby']
lucky_numbers = [4, 8, 15, 16, 23]
friends.extend(lucky_numbers)
print(friends)
friends.append('Creed')
print(friends)
friends.insert(1, 'Kelly')
print(friends)
friends.remove('Jim')
print(friends)
friends.pop()
print(friends)
print(friends.index('Oscar'))
print(friends.count('Kevin'))
names = ['Toby', 'Kevin', 'Oscar', 'Creed']
names.sort()
print(names)
names.reverse()
print(names)
names_copy = names.copy()
print(names_copy)
coordinates = (4, 5)
print(coordinates[0])
print(coordinates[1])
coordinates[0] = 10
def say_hi():
print('Hello User')
say_hi()
def say_hi(name, age):
print(f'Hello {name}, you are {age}')
say_hi('Mike', 35)
say_hi('Steve', 70)
def cube(num):
return num * num * num
result = cube(3)
print(result)
is_male = True
is_tall = True
if is_male and is_tall:
print('You are a tall male')
elif is_male and not is_tall:
print('You are a short male')
elif not is_male and is_tall:
print('You are not a male but are tall')
else:
print('You are not a male and not tall')
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
print(max_num(3, 4, 5))
print(max_num(140, 4, 5))
print(max_num(3, 4, 300))
num1 = float(input('Enter first number: '))
op = input('Enter operator (+, -, *, /): ')
num2 = float(input('Enter second number: '))
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '/':
print(num1 / num2)
elif op == '*':
print(num1 * num2)
else:
print('Invalid operator')
Dictionaries#
- A dictionary stores information as key-value pairs: each unique key maps to an associated value.
- Dictionaries are created with curly braces, and keys must always be unique.
month_conversions = {
'Jan': 'January',
'Feb': 'February',
'Mar': 'March',
'Apr': 'April',
}
print(month_conversions['Mar'])
print(month_conversions.get('Dec'))
print(month_conversions.get('Dec', 'Not a valid key'))
i = 1
while i <= 5:
print(i)
i += 1
print('Done with loop')
Mini Project: Guessing Game#
- We will pick a secret word, then let the user keep guessing until they either guess correctly or run out of tries.
- This combines while loops, if statements, and variables that track state across iterations.
secret_word = 'giraffe'
guess = ''
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != secret_word and not out_of_guesses:
if guess_count < guess_limit:
guess = input('Enter guess: ')
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print('Out of guesses, you lose')
else:
print('You win')
for letter in 'Learn Python':
print(letter)
for friend in ['Jim', 'Karen', 'Kevin']:
print(friend)
for index in range(10):
print(index)
for index in range(3, 10):
print(index)
friends = ['Jim', 'Karen', 'Kevin']
for index in range(len(friends)):
print(friends[index])
if True:
print('First iteration')
for index in range(5):
if index == 0:
print('First iteration')
else:
print('Not first')
def raise_to_power(base_num, pow_num):
result = 1
for index in range(pow_num):
result = result * base_num
return result
print(raise_to_power(3, 2))
print(raise_to_power(2, 3))
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0],
]
print(number_grid[0][0])
print(number_grid[2][1])
for row in number_grid:
for column in row:
print(column)
def translate(phrase):
translation = ''
for letter in phrase:
if letter.lower() in 'aeiou':
if letter.isupper():
translation = translation + 'G'
else:
translation = translation + 'g'
else:
translation = translation + letter
return translation
print(translate(input('Enter a phrase: ')))
Comments#
- A comment is a line of text in your code that Python completely ignores when running the program.
- Comments start with a hash sign, #, and are meant purely for humans reading the code.
- Use comments to explain a tricky line of code, leave yourself a note, or temporarily disable, or 'comment out', a line without deleting it.
number = int(input('Enter a number: '))
print(number)
try:
number = int(input('Enter a number: '))
print(number)
except:
print('Invalid input')
try:
number = int(input('Enter a number: '))
result = 10 / number
print(result)
except ZeroDivisionError:
print('Divided by zero')
except ValueError:
print('Invalid input')
with open('employees.txt', 'w') as employee_file:
employee_file.write('Jim Salesman\n')
employee_file.write('Dwight Salesman\n')
employee_file.write('Pam Receptionist\n')
print('File created')
employee_file = open('employees.txt', 'r')
print(employee_file.readable())
print(employee_file.read())
employee_file.close()
employee_file = open('employees.txt', 'r')
print(employee_file.readline())
print(employee_file.readline())
employee_file.close()
employee_file = open('employees.txt', 'r')
for employee in employee_file.readlines():
print(employee)
employee_file.close()
with open('employees.txt', 'a') as employee_file:
employee_file.write('Kelly Customer Service\n')
with open('employees.txt', 'r') as employee_file:
print(employee_file.read())
Modules#
- A module is simply a Python file whose functions and variables we can import and reuse in another file, or in this notebook.
- Python ships with many built-in modules, like math, which we already imported earlier in this lesson.
- Third-party modules, written by other developers, can be installed with pip, Python's package manager, by running 'pip install package-name' in a terminal.
Classes and Objects#
- A class lets us define our own custom data type, modeling a real-world thing like a student, a phone, or a chef.
- An object is a specific instance of a class, created from that class's template, with its own actual values.
class Student:
def __init__(self, name, major, gpa, is_on_probation):
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation
student1 = Student('Jim', 'Business', 3.1, False)
student2 = Student('Pam', 'Art', 2.5, True)
print(student1.name)
print(student1.gpa)
print(student2.gpa)
class Student:
def __init__(self, name, major, gpa):
self.name = name
self.major = major
self.gpa = gpa
def on_honor_roll(self):
if self.gpa >= 3.5:
return True
else:
return False
student1 = Student('Oscar', 'Accounting', 3.1)
student2 = Student('Phyllis', 'Business', 3.8)
print(student1.on_honor_roll())
print(student2.on_honor_roll())
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
'What color are apples?\n(a) Red\n(b) Purple\n(c) Orange\n\n',
'What color are bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n',
]
questions = [
Question(question_prompts[0], 'a'),
Question(question_prompts[1], 'c'),
]
def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print(f'You got {score} out of {len(questions)} correct')
run_test(questions)
class Chef:
def make_chicken(self):
print('The chef makes chicken')
def make_salad(self):
print('The chef makes a salad')
def make_special_dish(self):
print('The chef makes bbq ribs')
class ChineseChef(Chef):
def make_fried_rice(self):
print('The chef makes fried rice')
def make_special_dish(self):
print('The chef makes orange chicken')
my_chef = Chef()
my_chef.make_special_dish()
my_chinese_chef = ChineseChef()
my_chinese_chef.make_chicken()
my_chinese_chef.make_fried_rice()
my_chinese_chef.make_special_dish()
Recap#
- We installed Python and set up a Jupyter Notebook in VS Code as our environment for writing and running code.
- We covered variables, strings, numbers, lists, tuples, and dictionaries, along with the built-in functions and methods that work with each.
- We covered functions with parameters and return values, if/elif/else, comparison and logical operators, while loops, and for loops with range.
- We covered comments, try/except error handling, reading and writing files, modules, and classes with inheritance.
- 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.



