Variables, Expressions and Statements
Every program works with data. In order to
talk about data, you have to have names for it.
Every piece of data has a type. A data structure
includes the name of the piece of data, the value of it,
the type of
data it is and the actions you can do with that piece of
data. Example: a number called Age, with a value of
32, an integer type. The data type includes being able
to add, subtract, multiply, divide, compare, input, output,
assign that type of data.
When you run into a new data type, ask the questions -
what can I do with it?
- Values and Data Types
- Python has type() function to check
- note when the shell shows the quotes around a
string and when it does not
- float stored as 2 pieces: base (also known as mantissa) and exponent, (example 5.2 e 9)
- any characters can be inside a string
- single or double quotes delimiters
- even 3 quotes (allows for multiline strings)
- handy to be able to include single quotes in a string
that is delimited by double quotes and vice versa
- using comma in a print statement puts a space between
successive items to be printed
- commas are delimiters, not part of numbers
- x = 15,000 makes x a tuple with two elements (15,0)
- Type conversion functions
- also called 'type casting'
- int ()
- will produce an integer from
a floating point number or string - it does not change
the original value, it makes a new one
- does not round, it truncates towards zero
- float ()
- produces a floating point number from
an integer or a string
- both int() and float() assume that the string has a
valid number inside it - they do not handle letters!
- str () function
- will produce a string that contains the
numeric characters from a float or an integer
- why would you want to do that? you need to use a
string function on a number
- Assignment statements associates a name with a value
- note that = (assignment operator) does not mean the same as == (equality
- reference diagram
- variables have types and those can be changed by assignment statements
- use type function to see what type a variable is
- Variable Names and Keywords
- rules for identifier / variable names
- can't use keywords, or should NOT use - as identifiers
- Statements and Expressions
- difference between statement and expression
- len function
- Operators and Operands
- * / ** // % + -
- mod does operate on floats too
input function always returns a string
- have to use typecasts
to convert it to numbers "17" is not the same as 17.
- The information you need about an operator
- the semantics = what does it DO
- what arguments and how many does it have and what do they mean?
- what type(s) of arguments
- what type(s) does it return
- what is its precedence?
- Order of Operations
Pretty much same rules as algebra. () then ** then * and / and // and %
then + and - (addition and subtraction)
- if operators are at the same level, they are evaluated left to right
a variable can have many different values during the run of a program.
All that a computer can know is what the current value of a
variable is, not what it used to be.
- Updating Variables
- Counting statement x = x + 1. The variable has to be initialized
with some value before this statement will work.
- the first time any variable appears on the RIGHT hand side of the =
operator, it must already have a value or you get a runtime error
- increment, decrement - can use different amounts