Difference between revisions of "Introduction to Programming Week 2"
m |
(→Part 1: Datatypes) |
||
(6 intermediate revisions by one other user not shown) | |||
Line 3: | Line 3: | ||
Computers work with data, which might be a number, a piece of text, or a true/false value. | 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 [[integer]]s or [[floating point number]]s. 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. | *For the purposes of this class, numbers in [[Python]] are either [[integer]]s or [[floating point number]]s. 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 (<tt>'this is a string'</tt>) or double quotes (<tt> | + | *Text in Python is stored in a [[string]], and is delimited by either single quotes (<tt>'this is a string'</tt>) or double quotes (<tt>"this is also a string"</tt>). |
*A yes/no or true/false value in Python is called a [[boolean]]. <tt>True</tt> and <tt>False</tt> (note the capitalization) are special words in Python that represent these two cases. | *A yes/no or true/false value in Python is called a [[boolean]]. <tt>True</tt> and <tt>False</tt> (note the capitalization) are special words in Python that represent these two cases. | ||
Line 13: | Line 13: | ||
In numerical expressions Python understands the standard operator precedence rules (so for example <tt>1+2*3</tt> returns 7, not 9). | In numerical expressions Python understands the standard operator precedence rules (so for example <tt>1+2*3</tt> returns 7, not 9). | ||
− | Many of the same operations that work for numbers will also work for strings, but in different ways. For example, <tt>'Stan'+'ford'</tt> will evaluate to <tt>'Stanford'</tt> (this is called | + | Many of the same operations that work for numbers will also work for strings, but in different ways. For example, <tt>'Stan'+'ford'</tt> will evaluate to <tt>'Stanford'</tt> (this is called '''concatenation'''), and <tt>'spam'*3</tt> will evaluate to <tt>'spamspamspam'</tt>. |
− | We can use [[ | + | We can use [[type conversion functions|str]], a built-in function in Python, to convert numbers and other types of data into strings. For example, <tt>str(3.14)</tt> will evaluate to the string <tt>'3.14'</tt>, and <tt>str(1+2)</tt> will evaluate to <tt>'3'</tt>. |
====Part 3: Variables==== | ====Part 3: Variables==== | ||
Line 29: | Line 29: | ||
We made a program we called <tt>restaurant.py</tt>. Look in the script if you didn't follow along in class. | We made a program we called <tt>restaurant.py</tt>. 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 <tt>#</tt> symbol indicates to Python that everything after it in a line is a comment. | + | '''Comments''' are notes written for the benefit of humans reading your code, they're ignored by Python. The <tt>#</tt> symbol indicates to Python that everything after it in a line is a comment. |
====Part 5: Input and Output==== | ====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 [[type conversion functions|float]] function. | 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 [[type conversion functions|float]] function. | ||
+ | ==See Also== | ||
+ | [[Introduction to Programming Week 3]] | ||
[[Category:Introduction to Programming]] | [[Category:Introduction to Programming]] |
Latest revision as of 21:05, 15 August 2020
Contents
[hide]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.