Part One - Python Syntax
- The difference between high level languages and low level languages
- The python interpreter translates your code into instructions for the computer.
- Variables are used to store values
- Three basic data types are:
- Integers store non-decimal values. Ex: -5, 10, 82
- Floats store decimal values. Ex: -7.8, 10.0, 34.9876
- Booleans store two values: True or False
- Statements are commands that you issue to the interpreter.
- They need to be separated with whitespace.
- Comments are lines of code that don't do anything. They come in two types.
- Single line comments are written using the # symbol, called pound or hash
- Multi line comments are written using """, or triple quotes.
- Arithmetic operators are used to do math. They come in six basic types
- Addition uses the + symbol. Ex: a = 5 + 2 (a = 7)
- Subtraction uses the - symbol. Ex: a = 5 - 2 (a = 3)
- Multiplication uses the * symbol. Ex: a = 5 * 2 (a = 10)
- Division uses the / symbol Ex: a = 5 / 2 (a = 2)
- Exponentiation uses the ** symbol. Ex: a = 5 ** 2 (a = 25)
- Modulus uses the % symbol Ex: a = 5 % 2 (a = 1)
Part Two - Strings & Console Output
- The data type String is used to define a series of characters
- A String Literal is a string value. It is contained within a pair of quotes.
- Sometimes you will want to use a special character inside a string, such as the quote symbol. Use the / symbol in front of this symbol. This is called an escape character.
- You can access a specific character in a string by its index position. This is also called its subset or offset. Remember that you start count at zero.
- Strings have a few special methods that you can use. A method is a collection of code that performs a specific task. These methods are already written and ready for use.
- len() allows you to get the length of a string. Ex: print len() myString
- str() allows you to convert something to a string. Ex: print str() myInt
- lower() and upper() allow you to change the case of a string. Ex: print myString.upper()
- Two of the above methods use dot notation. This is because they are methods that are specific to Strings; they only work on a string. Len and Str work on multiple types.
- The editor is the window in which we write code
- The console is where your program outputs its results
- You can merge two strings using concatenation (the plus operator)
- Using str() to change an object to a string is called explicit string conversion. Later on in the course, when we study Java, we will call this idea "casting."
- String are immutable, meaning that methods like upper and lower won't actually change the original string. Instead, it returns a modified copy of the string that you can assign or print out.
- You can use %s to do some advanced string formatting methods. You may be asked to do this in a programming assignment, but will not be asked to do so without notes.
Part Three - Conditionals & Control Flow
- Control flow allows us to write programs with multiple outcomes.
- Comparison operators (also called compatators) let us compare numerical values
- Equal to (==)
- Not equal to (!=)
- Less than (<)
- Less than or equal to (<=)
- Greater than (>)
- Greater than or equal to (>=)
- Logical operators (also called boolean operators) let us make complex expressions.
- and means the same as it does in English. It evaluates to true when both sides of the and are true.
- or means and/or. It evaluates to true when one side or both sides of the or are true.
- not means the same as it does in English. It makes true become false, and false become true.
- Logical operators follow an order of operations
- not --> and --> or
- You can use parentheses to change the order of resolution
- Conditional statements alter the flow of a value base on user input or the environment
- Remember: whitespace matters. Use four spaces to indent a line.
- Only one kind of conditional in python - the if statement.
- You may modify it by using elif and else for more complex programs
- You can put one "if" inside of another "if". Doing so is called nesting.
- An algorithm is a series of specific steps to perform a task.
No comments:
Post a Comment