Introduction to Python
  • Introduction
  • Preface
  • Background
  • Installing Python
  • Getting Started
  • Basics
    • More Printing
    • Strings
    • Numbers
    • Exercises
    • Comments
  • Variables
    • Operators
    • Type Conversion
    • Exercises
    • A Brief Introduction to Lists
    • Game Exercise
  • Human Input
  • Functions
    • First Functions
    • Why Functions
    • Exercises
  • Indentation
  • Decisions
    • Booleans
    • Logical Operators
    • If Statements
    • Elif and Else Statements
    • Exercises
    • Rock, Paper, Scissors
    • Game Exercise
    • Game Exercise 2
  • Lists
  • Loops
    • For Loops
    • While Loops
  • More Data Structures
    • Tuples
    • Dictionaries
  • Pygame
  • Extra Content
    • Computers and Code
    • More About Python
    • For Loops with Range
    • List Slicing
Powered by GitBook
On this page
  1. Variables

Exercises

In the same my_variables.py file which you got open

  1. Create a variable long_hair. If your hair is long set it to True, otherwise set it to False. And remember, if you bought your hair it's yours! Don't let anyone tell you different.

  2. Create a variable called goat_music and that holds your favourite song. Using either string concatenation or the format function, print a message saying that the song in goat_music is the greatest song of all time. E.g. if goat_music = "Yellow by Coldplay" then print "Yellow by Coldplay is the Greatest Song of All Time".

  3. Let's start with two variables: wheels_on_bike = 2 and wheels_on_car = 4. Create a variable called total_wheels that contains the sum of the two previous variables.

  4. Create 3 variables mysterious_question, simulation and the_answer_to_everything. the_answer_to_everything should be equal to mysterious_question + simulation. Give mysterious_question and simulation 2 numerical values that would make the_answer_to_everything equal to 42. Print the_answer_to_everything to confirm.

  5. Create 3 variables for a triangle: side1, side2 and side3. Assign them values of 3, 4 and 5 respectively. What is the perimeter of this triangle? This triangle is a right triangle, with side1 and side2 as the base and height. Can you create a variable and store its area?

PreviousType ConversionNextA Brief Introduction to Lists

Last updated 5 years ago