AHS CODING CLUB

Welcome to AHS Code Club Mondays

This page will serve as a go to page for lesson, activites and code

Get in Touch

Lesson

Python Interpreter

Variables Review

What is a variable?

Variables in programming are like containers that store information or values, which can change as the program runs. Let’s use a video game example to explain this concept.

Imagine a Role-Playing Game (RPG)

hero
hero
Enemy1
enemy1

In an RPG, you might have a character with various attributes like health, mana, inventory, and experience points. Each of these can be represented by a variable.

player_health = 100 player_name = "Harry" player_score = 0

How Variables Change

player_health = player_health - 10 player_score = player_score + 100 # Add 100 points to the score

Here, apples is the name of the box (the variable), and 5 is what’s inside (the value). Now, anytime you ask Python, “How many apples do I have?” it will tell you 5.

Strings

What is a string?

A string in Python is like a sentence or a word that you want the computer to remember. Instead of storing numbers like 5 or 10, a string stores letters, symbols, or words. You make a string by putting text between quotes, like this:


name = "Alice"

In this case, name is the variable, and "Alice" is the string stored inside it. Strings can be any words or sentences, like "Hello, world!" or "I have 3 dogs!". You can even have empty strings with no letters at all, like this:


empty_string = ""

Think of strings as pieces of text you can save and use whenever you need them, like writing a message, displaying someone's name, or even asking questions!

To make Python show output, you use the print() function. For example:


print("Hello, world!")

This line of code tells Python to show the text "Hello, world!" as output. When you run this, the computer displays:


Hello, world!

Math With Strings

first_name = "Lane "
last_name = "Wagner"
full_name = first_name + last_name
print(full_name)
# prints "Lane Wagner"
three_first = first_name * 3 print(three_first)

Numbers

Numbers are not surrounded by quotes when they're declared. An integer is a number without a decimal part:
x = 5 # positive integer
y = -5 # negative integer
A float is a number with a decimal part:
x = 5.2
y = -5.2

Here are some examples of common mathematical operators in Python syntax.

summation = a + b # Addition
difference = a - b # Subtraction
product = a * b # Multiplication
quotient = a / b # Division
remainder = a % b # Modulus Operator

Booleans

A "Boolean" (or "bool") is a type that can only have one of two values: True or False. As you may have heard, computers really only use 1's and 0's. These 1's and 0's are just True/False boolean values. is_tall = True is_short = False

Output

In Python, output is what you see as a result when the program runs. When you tell Python to "print" or "show" something, it displays it as output on the screen.

You can also use print() to show numbers, words, or the values of variables. Here’s an example:


apples = 5
print(apples)

The print() function is the main way to create output in Python, letting you see what’s going on in your program!

    

    a = int(input('Enter 1st number: '))
    b = int(input('Enter 2nd number: '))
    print(f'Sum of {a} and {b} is {a + b}')

Input

In Python, input is when you ask the user (the person using the program) to type something. This way, your program can get information from the user to use in the code. You use the input() function to get input. Here’s a simple example:


name = input("What is your name? ")
print("Hello, " + name + "!")

In Python, input is when you ask the user (the person using the program) to type something. This way, your program can get information from the user to use in the code. You use the input() function to get input.

Here’s a simple example:


name = input("What is your name? ")
print("Hello, " + name + "!")

In this code:


input("What is your name? ") shows a message asking for the user’s name.
Whatever the user types in is stored in the variable name.
Then print("Hello, " + name + "!") uses the name variable to greet the user.
If the user types “Alice,” the output will be: Hello, Alice!

The input() function always takes in text as input, so you can use it to ask questions, collect data, and make your programs interactive!


Input will always return a string. If you enter 3 in response to the input prompt what you will get is "3". If you want to do any calculations with this you have to convert it to an integer using int()

#This program Asks User for 2 Numbers and then Multiplies them
number1 = input("Please enter a number: ")
number2 = input("Please enter a second number")

number1 = int(number1)  #Could do it all in one line: number1 = int(input("Please enter a number: "))
number2 = int(number2)

print(number1*number2)

Comments

In Python, comments are notes you write in your code to explain what it does or to remind yourself of something. Python ignores comments when it runs the program, so they don’t affect how the code works.

To write a comment, you start the line with a # symbol. Anything you type after # on that line is a comment. Here’s an example:


# This is a comment. It explains the next line of code.
print("Hello, world!") # This line prints a message

In this example:


# This is a comment. It explains the next line of code. is a comment.
# This line prints a message is another comment after the print() function.

Comments are helpful for:

Explaining what the code does so others (or your future self) can understand it.

Making notes about things you need to change later.

Turning off parts of code temporarily while testing (by adding # in front of those lines).

They’re like little reminders for programmers, and they make your code easier to read and understand!

Keywords