PythonCheatsheet.Org Flashcards
Studying https://www.pythoncheatsheet.org/cheatsheet/basics
Math Operators
From highest to lowest precedence:
Augmented Assignment Operators
Walrus Operator
The Walrus Operator allows assignment of variables within an expression while returning the value of the variable
The Walrus Operator, or Assignment Expression Operator was firstly introduced in 2018 via PEP 572, and then officially released with Python 3.8 in October 2019.
Data Types
Concatenation and Replication
Variable naming rules
- it can only be on word
- it can only use letters, numbers, and the underscore (
_
) character - It can’t begin with a number
- variables starting with an underscore (
_
) are considered as “unuseful”
Comments
The print()
Function
The print()
function writes the value of the argument(s) it is given. […] it handles multiple arguments, floating point-quantities, and strings.
Strings are printed without quotes, and a space is inserted between items, so you can format things nicely:
The end
keyword
The keyword argument end
can be used to avoid the newline after the output, or end the output with a different string:
The sep
keyword
The keyword sep
specify how to separate the objects, if there is more than one
The input()
Function
- This function takes the input from the user and converts it into a string:
-
input()
can also set a default message without usingprint()
: - It is also possible to use formatted strings to avoid using
.format
:
The len()
Function
Evaluates to the integer value of the number of characters in a string, list, dictionary, etc.
*don’t use it to test emptiness
Should you use len()
to test emptiness?
No, Test of emptiness of strings, lists, dictionaries, etc., should not use len, but prefer direct boolean evaluation.
The str()
, int()
, and float()
Functions
These functions allow you to change the type of variable. For example, you can transform from an integer or float to a string. Or from a string to an integer or float.
abs()
Return the absolute value of a number.
aiter()
Return an asynchronous iterator for an asynchronous iterable.
all()
Return True
if all elements of the iterable are true.
any()
Return True
if any element of the iterable is true.
ascii()
Return a string
with a printable representation of an object.
bin()
Convert an integer number to a binary string.
bool()
Return a Boolean value.
breakpoint()
Drops you into the debugger at the call site.
bytearray()
Return a new array of bytes.
bytes()
Return a new “bytes” object.