2-Variables, Expressions, and Statements
2-Variables, Expressions, and Statements
Statements
Values and types
• A value is one of the basic things a program works with, like a letter
or a number.
• Some values belong to different types: 2 is an integer, and “Hello,
World!” is a string, so called because it contains a “string” of letters.
• If you are not sure what type a value has, the interpreter can tell you.
Values and types
• Strings belong to the type str and integers belong to the type int. Less
obviously, numbers with a decimal point belong to a type called float,
because these numbers are represented in a format called floating
point.
• What about values like “17” and “3.2”? They look like numbers, but
they are in quotation marks like strings.
• When you type a large integer, you might be tempted to use commas
between groups of three digits, as in 1,000,000. This is not a legal
integer in Python.
Variables
• One of the most powerful features of a programming language is the
ability to manipulate variables. A variable is a name that refers to a
value.
• An assignment statement creates new variables and gives them
values:
Variables
• To display the value of a variable,
you can use a print statement:
• To obtain the same answer in Python 3.0 use floored ( //) division.
Expressions
• An expression is a combination of values,
variables, and operators. A value all by itself is
considered an expression, and so is a variable.