# Introduction to Programming Week 2

## Summary

#### Part 1: Datatypes

Computers work with data, which might be a number, a piece of text, or a true/false value.

• For the purposes of this class, numbers in Python are either integers or floating point numbers. Unlike humans, who use base 10, computers store numbers in binary (base 2), which may lead to very small rounding errors in floating point calculations.
• Text in Python is stored in a string, and is delimited by either single quotes ('this is a string') or double quotes ("this is also a string").
• A yes/no or true/false value in Python is called a boolean. True and False (note the capitalization) are special words in Python that represent these two cases.

There are many other types of data Python can understand. Later in the course we'll learn about more, and how to make our own datatypes.

#### Part 2: Expressions

Data and operators can be combined to form expressions which Python can evaluate. The data used in an expression are also called the operands. Python also understands several types of relational operators, which always return a boolean.

In numerical expressions Python understands the standard operator precedence rules (so for example 1+2*3 returns 7, not 9).

Many of the same operations that work for numbers will also work for strings, but in different ways. For example, 'Stan'+'ford' will evaluate to 'Stanford' (this is called concatenation), and 'spam'*3 will evaluate to 'spamspamspam'.

We can use str, a built-in function in Python, to convert numbers and other types of data into strings. For example, str(3.14) will evaluate to the string '3.14', and str(1+2) will evaluate to '3'.

#### Part 3: Variables

Variables are used to remember the results of previous computations. Variables are assigned values with an assignment statement, which has the following form: (variable name) = (data or expression). Unlike expressions, statements ask Python to perform a task and don't evaluate to a value.

Variables can only contain upper and lowercase letters (Python is case-sensitive) and _ (the underscore character). Hence, because we can't have spaces in variable names a common convention is to capitalize the first letter of every word after the first. For example, myName, or debtAmountWithInterest.

Variables can be used in expressions in place of the data stored in them. In particular, if total has the value 5, Python has no problem with the following:

```total = total + 1
```

The right side is always evaluated first: total+1 will evaluate to 6, which will be stored in the updated total variable. In Python, = is the assignment operator, not an equality test (for that see relational operators).

#### Part 4: A Real Program

We made a program we called restaurant.py. Look in the script if you didn't follow along in class.

Comments are notes written for the benefit of humans reading your code, they're ignored by Python. The # symbol indicates to Python that everything after it in a line is a comment.

#### Part 5: Input and Output

The print function outputs text to the screen, while the input function prompts the user to input a string. To convert strings to floating point numbers we can use the float function.

Invalid username
Login to AoPS