Python: Variables, Strings and Numbers

  1. Introduction to Python
  2. Python: Choosing a Text Editor or IDE
  3. Python: Hello World
  4. Python: Variables, Strings and Numbers
  5. Python: Variable Naming
  6. Python: Math, Familiar
  7. Python: Math, Less Familiar
  8. Python: Mathematical Order of Operations
  9. Python: Introducing PEP 8
  10. Python: Text Concatenation
  11. Python: if Statements and Comparison Operators
  12. Python: else and elif statements
  13. Python: Testing Multiple Conditions
  14. Python: Testing Sets of Conditions
  15. Python: Nested if Statements
  16. Python: Lists
  17. Python: Adding To and Changing Lists
  18. Python: Lists: Take a Slice, Delete Elements, Popping Elements
  19. Python: Tuples
  20. Python: for Loops
  21. Python: Nested for Loops
  22. Python: Capturing and Formatting User Input
  23. Python: Dictionaries
  24. Python: Functions
  25. Python: While Loops
  26. Python: Creating and Using Classes
  27. Python: Data Files
  28. Python: Modules
  29. Python: CSV Files
  30. Python: JSON Files
  31. Python: Errors and Exception Handling
  32. Python: Using Pexpect
  33. Python : Using Pexpect :
  34. Python : Using Pexpect:
  35. Python: DCL Conversion to Python

1. Variables: What They Look Like

Go to Chapter 2 of A Smarter Way.

Variable names can contain letters, numbers and underscores – nothing else.

And variable names must always begin with a letter or underscore, but never a number.

Variable names can never, ever have spaces in them!

2. Variables: Strings

When you assign a string value to a variable (or create a variable and simultaneously assign the value), remember that strings go in quotes.

name = "Barney"

Note the spaces around the equal sign (which is actually the assignment operator).  These are not absolutely required, but again, they’re dictated by Python’s stern style requirements. So use them. Always.

As for the quotation marks, you can use single quotes or double quotes; they both mean the same thing in Python.

You can print out the values of variables like this:


You can’t put variables inside quotes: they must always be in the clear.

print("name") # NO this is wrong!

print(name) # YES do it this way


Online, go to http://introtopythonorg/hello_world.html. Run the simple examples.

Go to and review the exercises.

  1. Continue editing your script,
  2. Create a new variable that contains “Author: “.
  3. Create a new variable, my_name, and populate it with your name (or any name).
  4. Be sure to handle it as a string.
  5. Add another print statement that outputs these variables on one line.

3. Variables: Numbers

Go to Chapter 3 of A Smarter Way.

When you’re assigning a value to a variable, and the value is a number, numbers never go in quotes.

number_of_cats = 2

You can print a numeric variable like this:


If a variable is a number, you can use the variable like a number in math:

number_of_cats = number_of_cats + 3


Go to and review the exercises.

  1. Continue editing your
  2. Create a new variable, my_year,  that contains the current year.
  3. Be sure to handle the variable as a number.
  4. Print “Copyright ” and  the year.

By now your should be printing what looks like the beginning of a paper: a title, an author and a year.

4. Now go to Variables, Strings and Numbers online:

Particularly look at Changing case and Comments, for now. We’ll look at concatenating strings later.

my_name = "blarney"



# This is a comment

print(my_name) # This is an inline comment

This is a 
multi-line comment


At the bottom of the page, note the section, Exploring the Python Community

  1. Continue editing your file.
  2. Print the label “Author’s name: ” followed by my_name in lowercase, uppercase and title case. Use three lines to do this.
  3. Create a new variable, my_degree, that contains the value ” Ph.D.”.
  4. Concatenate my_name and my_degree so you can print them. Do you need another variable or can you concatenate inside the print statement?