What is floor division (or div), what are value of (-11) // 4 ?
Mathematical division that rounds down to nearest integer. The floor
division operator is //.
-11 // 4 is -3;
-11 // 3.5 is -4.0, because that is -2.75 and -3.14 rounded downward.
The % operator returns the remainder of the division.
Operators with mixed type operand’s convert the integer operand
to floating point, int → float.
Use variables:
Use undefined variable, will raise NameError:
In interactive mode, the last printed expression is assigned to the variable
==_==, and it should be read-only (please avoid using it as real variable).
Python support various types of numbers such as: Int, Float,
Decimal, Fraction, Complex number’s (complex using j or J
suffix - 3+5j).
In Python all these types are objects, so they have methods and attributes.
Python strings (manipulate text):
In interactive mode escape sequences are not recognized, so
use print() function if you want interpreter them.
You can disable escaping by using raw strings:
NOTE: A raw string may not end in an odd number of \ characters, because it will
escape last quote.
Unlike other languages, special characters such as \n have the
same meaning with both single ('...') and double ("...") quotes. The
only difference between the two is that within single quotes you don’t need to
escape " (but you have to escape ') and vice versa.
Strings can be multiple lines, wrapped by triple double quotes
(""") or triple single quotes ('''). You can use \ character to prevent
automatically inserting new line in output, in functions or methods at the top
this usually using for documentation (docstrings).
You can also concatenate strings with ==+== operator or concatenate by
breaking them into new lines or space with quoting each string.
* operator used for repeating string (multiplication).
Strings can be indexed (subscripted). In python no separate character type, a
character is a string of size one.
Positive indexing = length of string - 1
Simple example of random indexing checking game:
Negative indexing, -1 is last character
Strings can be sliced, to obtain substring
What you see if you use this slice word[0:2], where word is “Python”
Characters from position 0 (included) to 2 (excluded), 'Py'
What you see if you use this slice word[2:5], where word is “Python”
Characters from index 2 (included) to index 5 (excluded). 'tho'
Note: s[:i] + s[i:] == ==s==, start is always included, and the end always
excluded.
Attempting to use invalid index in range slice handled with or without exception?
Gracefully, without raising an exception.
To remember how slices work, you can use this table. Think indices are pointing
between characters.
+---+---+---+---+---+---+
| H | e | l | l | o | , |
+---+---+---+---+---+---+
.0 .1 .2 .3 .4 .5 6 # positive indices, i-j chars between i and j
-6. -5. -4. -3. -2. -1. # negative indices
For positive indices, the length of a slice is the difference of the
indices or range, if both are within bounds. For example, the length of
word[1:3] is 2.
Is it possible to use index outside of string bounds?
No, it will raise IndexError exception. So it’s good idea to check bounds
before using index. "Test string"[11].
Python strings are ==immutable==. You can’t assign new
value to character in string.
Most versatile data type is list. In Python list can contain elements of
different types, but usually used one type of elements. python squares = [1, 4, 9, 16, 25] print(squares) # [1, 4, 9, 16, 25] squares = ["1", 4, "9", 16, "25"] print(squares)
Lists almost like string (but mutable) and
iterators can be indexed and sliced.
All slice operations over list return a new list (shallow copy) containing
the requested elements.
Lists also support operations like concatenation.
List are mutable, unlike strings.
Is it possible to change multiple elements in list at once?
You can also do assignments to lists (inserting/replace), which can change their
size or clear them entirely.
Can you write Fibonacci algorithm?
We just print values in fibonacci sequence, not store them in list.
Constraints: 0 - 21.
Each number is the sum of the two preceding ones.
The sequence commonly starts from 0 and 1.
So for example we need to build Fibonacci sequence with 7 elements.
0 → first initial number
1 → second initial number
1 → sum of 2. and 1.
2 → sum of 3. and 2.
3 → sum of 4. and 3.
5 → sum of 5. and 4.
8 → sum of 6. and 5.
\
Results:
0
1
1
2
3
5
8
13
21
---
0
1
1
2
3
5
8
13
21
Order of Evaluation in Python expression for operands with same priority?
In Python, the left operand evaluated before the right operand if they have
same priority.
Non-zero, zero, zero length or None values in Python (and C)
considered as ==boolean== (at least in if/else
statements).
-3**2 in Python is?
Since ** operator has higher precedence than -, -3**2 will be interpreted as
-(3**2) and thus result is -9 (expected 9). For correct calculation, you can use parenthesis (-3)**2.