get consultation

Variables

Variables are used to contain data such a numbers or strings.

The keyword print( ) is used to print data to the screen.

variables.py
# This is a comment
age = 15

# Simple math
age = age + 2

# To print data, use the print( ) method
print(age)

# Strings has have single-quotes ('string') or double-quotes ("string")
name = 'Steve'
my_name = "David"

# String building
name = name + " Johnson"
print(name)

# String building
name = "Sir " + my_name + " Johnson""
print(name)

# convert strings to integers
input = '101'
number = int(input)
number = number + 2

print(number)

NOTE:You need to convert strings to integers (numbers) before you can do any kind of math on them (addition, subtraction, multiplication, etc).

What happens if you just add a number (integer) to a string?

variables.py
input = '101'
number = input + 2

print(number)

More printing

printing.py
# simple printing
print("Hello")
print(25)

# complex printing
print('Hello my name is Steve')
print('Hello', 'my', 'name', 'is', 'Steve')
print('I', 'will', 'be', 5, 'next year')

Links

Exercises (5 mins)

  1. Create a new program

    1. Create a new variable named account_balance

    2. Set account_balance to 0

    3. Add 100 to account_balance

    4. Print the account_balance

    Output:

    100
    
  2. Create a new program

    1. Create a new variable named name

    2. Set name to your real name (or just use Steve or Bob)

    3. Update name to add a "Mr." or "Ms." in front of your name

    4. Print the name

    Output:

    Mr. Steve Johnson