Skip to main content

Unit 2 · Python mode... Activate! · Lesson 13 of 57

Magical Mayhem

It has to be magic!

When I first learned Python, I swore that the way variables worked was like some sort of magical teleportation.

(I was like 10, give me a break)

The same way you can kind of just write a variable in code like:

company = "CyberAcad"

Assuming you defined, or set a value for the variable company, you can just type...

company

... and that's completely valid code!

Python won't do anything with it though because you haven't told it to do anything!

Try it out!

  • Make a variable pi, with type float, that holds the value 3.14!

  • Type pi on the next line, and see what the program does!

Python exercise

Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.

Okay, here comes the magic!

All this time, we've been printing stuff to the screen by just putting it in a string, like this, right?

print("Hello World!")

What if I told you, you could print out the value of a variable by referencing it!

The value literally TELEPORTS!

See if you can figure out how to do it.

  • Make a variable called company_name, of type string, holding the value CyberAcad
  • Print out company_name

Python exercise

Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.

Do this lesson for real

With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.