Variables in Python

Introduction

Numbers and character strings are important data types in any Python program

  • These are the fundamental building blocks we use to build more complex data structures.

Variables

A variable is a named storage location in a computer program.

  1. There are many different types of variables, each type used to store different things.
  2. You ‘define’ a variable by telling the compiler:
    1. What name you will use to refer to it.
    2. The initial value of the variable.
  3. You use an assignment statement (=) to place a value into a variable.

Variable Definition

To define a variable, you must specify an initial value.

****

The assignment statement


  1. Use the assignment statement '=' to place a new value into a variable cansPerPack = 6 # define & initializes the variable cansPerPack
  2. The left-hand side of an assignment statement consists of a variable
  3. The right-hand side is an expression that has a value
  4. Beware: The “=“ sign is NOT used for comparison:
    1. It copies the value on the right side into the variable on the left side
    2. You will learn about the comparison operator in the next chapter

Assignment syntax

*****

Why different types?


No comments:

Post a Comment