Search 83,403 tutors
FIND TUTORS

Chapter 2, How to Think Like a Computer Scientist, Learning with Python 3 (RLE)

Chapter 2 of How to Think Like a Computer Scientist begins actually exploring Python and its features.
 
The chapter discusses variables, expressions, and statements. It starts out discussing data types and using the type() function to determine the type of some literals, like strings, integers, and floats. It also discusses using different kind of string delimiters.
 
Variables have a name and can be created by using the name you want to give the variable, the assignment token (=), and a value, like so:
 
lunch = "sandwich"
weight = 150
 
Variable names have to begin with a letter or underscore (_) and can't be one of Python's reserved keywords (http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html#variable-names-and-keywords).
 
Statements are instructions that python can perform. Examples given are while, for, import and others.
 
Expressions are combinations of variables, values, and operators which Python can evaluate to create a result.
 
Operators are tokens or symbols which indicate specific operations, such as adding (+).
 
There are built in functions for converting between types, especially when converting from a number represented as a string (such as a number typed in by the user in response to an input statement) and a number represented internally to Python as an int or a float. The int(expression) statement attempts to turn the expression into an integer, the float(expression) statement does the same except converting to float, and the str(expression) statement converts the statement to a string.
 
Operations in an expression are evaluated according the usual mathematical order of operations, PEMDAS.
 
The input(string) statement prints a string (for a prompt) and then reads the user's input and returns it, where it can be assigned to a variable or used directly.
 
expressions and statements can be combined.
 
The modulus operator is another operator, which returns the remainder of integer division. The chapter ends with a glossary and a list of exercises.
 
My answers to the exercises are in my next blog post.