Python Flashcards
Learn and remember Python syntax and functions
Type
How Python represents different types of data. Int short for integer, Str short for string, and Float for floats are all examples of types.
Type Casting
This is converting the type of data from one form to another.
Boolean
A Boolean represents one of two values: True or False.
Expressions
A type of operation that computers perform like basic arithmetic operations like adding multiple numbers.
The numbers are called “operands” and the math symbols like the plus signs are “operators”.
Math Operators
+ = Addition
- = Subtraction
* = Multiplication
/ = Division
// = Integer division (results rounded down)
Variables
Variables store values using the equals symbol (=) as an “assignment” operator.
Python Versions
The two popular versions of Python currently in use are Python2 and Python 3. Support for Python 2 is now being dropped however in favour of Python 3.
Comments
You can write a non-executable comment directly in front of code denoted with a #.
Strings
Strings are composed of a sequence of characters. They can be contained in either single or double quotes.
They can be spaces, digits or special characters.
Slicing Strings
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
len()
The len() command returns the length of a string.
Concatenate
To concatenate or join variables and strings together you use the plus (+) symbol.
Tuples
Tuples are used to store multiple items in a single variable.
Tuples are one of 4 built-in types in Python with the other 3 being List, Set, and Dictionary.
Tuples are written in round brackets (). A tuple is ordered and unchangeable.
Escape Sequences (Characters)
To insert characters that are illegal in a string, use an escape character.
An escape character is a backslash \ followed by the character you want to insert.
Two backslashes \ put a “backslash” in your string.
An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
' (Escape Character)
Single Quote
\ (Escape Character)
Backslash
\n (Escape Character)
New Line
\r (Escape Character)
Carriage Return
\t (Escape Character)
Tab
\b (Escape Character)
Backspace
\f (Escape Character)
Form Feed
\ooo (Escape Character)
Octal value
\xhh (Escape Character)
Hex value
capitalize() (String Manipulation)
Converts the first character to upper case
casefold() (String Manipulation)
Converts string into lower case
center() (String Manipulation)
Returns a centered string