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

Variables

Variables are names for stored values in a program. They can store a variety of data. Create a new file my_variables.py and type the following:

# Variables can be strings i.e. text values
my_name = 'Rico Suave'
# Variables can be numbers, like integers (ints)
total_cars_owned = 10
# There are also floats as well
university_gpa = 3.8
# Somethings we just need to know if something is True or False
can_dance = True
# We can display variable values by using string's format method and a placeholder - {}
print('Hola, me llamo {}'.format(my_name))
print('I have a total of {} cars'.format(total_cars_owned))

Can you print your GPA in a sentence as well? Some things to note:

  • You don't need underscores in your variable names. It is standard practice in Python to separate words in variable names with them. You should maintain this standard.

  • You can change the value of the variable at any time, that's why we call them variables after all. You can even change a variable from one type to another. For example, university_gpa can be set to "3.8" afterwards. This is generally discouraged as consistency is valuable in programming and the usual operators may not work with the variable's new type.

  • You can always write my_name='Rico Suave', but that's just not as readable as my_name = 'Rico Suave'. Be nice to yourself and to other programmers who may read your code, use spaces.

PreviousCommentsNextOperators

Last updated 6 years ago