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
count() (String Manipulation)
Returns the number of times a specified value occurs in a string
encode()
endswith() (String Manipulation)
Returns true if the string ends with the specified value
find() (String Manipulation)
Searches the string for a specified value and returns the position of where it was found
format() (String Manipulation)
Formats specified values in a string
index() (String Manipulation)
Searches the string for a specified value and returns the position of where it was found
isspace() (String Manipulation)
Returns True if all characters in the string are whitespaces
isnumeric() (String Manipulation)
Returns True if all characters in the string are numeric
isdigit() (String Manipulation)
Returns True if all characters in the string are digits
islower() (String Manipulation)
Returns True if all characters in the string are lower case
isdecimal() (String Manipulation)
Returns True if all characters in the string are decimals
isalpha() (String Manipulation)
Returns True if all characters in the string are in the alphabet
upper() (String Manipulation)
Converts a string into upper case
strip() (String Manipulation)
Returns a trimmed version of the string
RegEx Module
re is a package built into Python that can be used to work with regular Expressions.
It’s imported using the “import re” command.
findall (RegEx)
Returns a list containing all matches
search (RegEx)
Returns a Match object if there is a match anywhere in the string
split
Returns a list where the string has been split at each match
sub
Replaces one or many matches with a string
index()
This returns the position at the first occurrence of a specified value
List
A list stores multiple items in a single variable or integer index. This is 1 of the 4 in-built Python data types with the other 3 being Set, Dictionary and Tuple.
Dictionary
Dictionaries are used to store data values in key, value pairs.
A dictionary is a collection which is ordered (Python 3.7), changeable and does not allow duplicates.
Dictionaries are written with curly brackets {} and have keys and values.
Sets
Sets are used to store multiple items in a single variable.
They’re 1 of the 4 built-in data types in Python with the other 3 being Tuple, Dictionary and List.
A set is a collection that’s unordered, unchangeable, and unindexed.
They’re written using curly brackets {}.
union()
The union() method returns a set that contains all items from the original set, and all items from the specified set(s).
You can specify as many sets as desired, separated by commas.
In addition to sets union() can be used on any iterable object.
If an item is present in more than one set, the result will contain only one appearance of this item.
Comparison Operators
Comparison operations compare some values and operands, then based on some condition they produce a Boolean.
== (Comparison Operator)
Equal
!= (Comparison Operator)
Not Equal
> (Comparison Operator)
Greater than
< (Comparison Operator)
Less than
> = (Comparison Operator)
Greater than or equal to
<= (Comparison Operator)
Less than or equal to
Branching
Branching allows us to run different statements for a different input.
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define the scope in the code. Other programming languages often use curly-brackets for this purpose.
If statement
Python conditions such as == (equals), < (less than) can be written in “if statements” and loops.
An example of this is;
a = 33
b = 200
if b > a:
print(“b is greater than a”)
Elif
This is Python’s way of saying “if the previous conditions were not true, then try this condition”.
Else
The else keyword catches anything which isn’t caught by the preceding conditions.
Or (Logical Operator)
The or keyword is a logical operator, and is used to combine conditional statements.
It returns True is one of the statements is true.
Not (Logical Operator)
The not keyword is a logical operator and is used to reverse the result of the conditional statement.
It returns False if the result is true.
And (Logical Operator)
The And operator takes in 2 Boolean values and produces a new Boolean value if 2 conditions are true.
Returns True if both statements are true.
Logical Operators
Logical operators (and, not, or) are used to combine conditional statements (to produce Boolean values).