2 Key takeaways Flashcards
notations for representing some fixed values in code. Python has various types of literals - for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals, e.g., “I am a literal.”).
Literals
system of numbers that employs 2 as the base. Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is 10 in decimal.
binary system
Octal and Hexadecimal facts
Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases respectively. The hexadecimal system uses the decimal numbers and six extra letters.
Integers (or simply ints) are one of the numerical types supported by Python
They are numbers written without a fractional component, e.g., 256, or -1 (negative integers).
one of the numerical types supported by Python. They are numbers that contain (or are able to contain) a fractional component, e.g., 1.27.
Floating-point numbers (or simply floats)
To encode quote inside a string you can either use
escape character, e.g., ‘I'm happy.’, or open and close the string using an opposite set of symbols to the ones you wish to encode, e.g., “I’m happy.”
to encode an apostrophe, and ‘He said “Python”, not “typhoon”’ to encode a (double) quote.
apostophre
two constant objects True and False used to represent truth values (in numeric contexts 1 is True, while 0 is False.
Boolean Values
There is one more, special literal that is used in Python: the None literal. This literal is a so-called NoneType object, and it is used to represent
the absence of a value
“Hello “, “007”
strings/string literals.
“1.5”
2.0
528
False
The first is a string,
second is a numerical literal (a float),
third is a numerical literal (an integer),
fourth is a boolean literal.
What is the decimal value of the following binary number?
1011
It’s 11, because
(20) + (21) + (2**3) = 11
symbol of the programming language, which is able to operate on the values.
operator
For example, just as in arithmetic, the + (plus) sign is the operator which is able to add two numbers, giving the result of the addition.
operators which are associated with the most widely recognizable arithmetic operations:
+, -, *, /, //, %, **
Please excuse my dear aunt sally
parentheses
exponents
multiplication
division
addition
subtraction
data and operators when connected toghether form
expressions. The simplest expression is a literal itself.
combination of values (or variables, operators, calls to functions ‒ you will learn about them soon) which evaluates to a certain value, e.g., 1 + 2.
expression
special symbols or keywords which are able to operate on the values and perform (mathematical) operations, e.g., the * operator multiplies two values: x * y.
Operators
+ (addition), - (subtraction), * (multiplication), / (classic division ‒ always returns a float), % (modulus ‒ divides left operand by right operand and returns the remainder of the operation, e.g., 5 % 2 = 1), ** (exponentiation ‒ left operand raised to the power of right operand, e.g., 2 ** 3 = 2 * 2 * 2 = 8), // (floor/integer division ‒ returns a number resulting from division, but rounded down to the nearest whole number, e.g., 3 // 2.0 = 1.0)
Arithmetic operators in Python
operator is an operator with only one operand, e.g., -1, or +3.
unary
operator is an operator with two operands, e.g., 4 + 5, or 12 % 5.
binary
Some operators act before others – the hierarchy of priorities:
the ** operator (exponentiation) has the highest priority;
then the unary + and - (note: a unary operator to the right of the exponentiation operator binds more strongly, for example: 4 ** -1 equals 0.25)
then *, /, //, and %;
and, finally, the lowest priority: the binary + and -.
are always calculated first, e.g., 15 - 1 * (5 * (1 + 2)) = 0.
Subexpressions in parentheses
The exponentiation operator uses
right-sided binding, e.g., 2 ** 2 ** 3 = 256.
print((2 ** 4), (2 * 4.), (2 * 4))
16 8.0 8
print((-2 / 4), (2 / 4), (2 // 4), (-2 // 4))
-0.5 0.5 0 -1
print((2 % -4), (2 % 4), (2 ** 3 ** 2))
-2 2 512
(2 ** 3 ** 2))
same as
2 ** (3 ** (2 ** 1))
2 ** (3 ** (2 ** 1))
= 2 ** (3 ** 2)
= 2 ** 9
= 512
named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time. (2.1.4.1)
Variable
A legal identifier name must be a non-empty sequence of characters, must begin with the underscore(_), or a letter, and it cannot be a Python keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python are case-sensitive. (2.1.4.1)
Each variable must have a unique name - an identifier
means you don’t need to declare variables in it. (2.1.4.3) To assign values to variables, you can use a simple assignment operator in the form of the equal (=) sign, i.e., var = 1.
Python is a dynamically-typed language
(shortcut operators) to modify values assigned to variables, e.g., var += 1, or var /= 5 * 2. (2.1.4.8)
You can also use compound assignment operators
You can assign new values to already existing variables using the assignment operator or one of the compound operators, e.g.: (2.1.4.5)
var = 2
print(var)
var = 3
print(var)
var += 1
print(var)
You can combine text and variables using the + operator, and use the print() function to output strings and variables, e.g.: (2.1.4.4)
var = “007”
print(“Agent “ + var)
var = 2
var = 3
print(var)
3
my_var
m
101
averylongvariablename
m101
m 101
Del
del
my_var
m
101 # incorrect (starts with a digit)
averylongvariablename
m101
m 101 # incorrect (contains a space)
Del
del # incorrect (is a keyword)
a = ‘1’
b = “1”
print(a + b)
11
a = 6
b = 3
a /= 2 * b
print(a)
1.0
2 * b = 6
a = 6 → 6 / 6 = 1.0
function sends data to the console
print()
gets data from console
input()
function comes with an optional parameter: the prompt string. It allows you to write a message before the user input, e.g.:
name = input(“Enter your name: “)
print(“Hello, “ + name + “. Nice to meet you!”)
input()
When the input() function is called, the program’s flow is stopped
the prompt symbol keeps blinking (it prompts the user to take action when the console is switched to input mode) until the user has entered an input and/or pressed the Enter key.