Module 1 & 2
Module 1 & 2
integers, that is, those which are devoid of the fractional part; WHOLE NUMBERS
floating-point numbers (or simply floats), that contain (or are able to contain)
the fractional part. BASTA ETO UNG MAY DECIMAL
4.0 = .4 = 4.
3 x 10 8 = 3E8
print(0.0000000000000000000001)
Output = 1e-22
The second convention allows us to use hexadecimal numbers. Such numbers should be
preceded by the prefix 0x or 0X (zero-x).
from right to left: first 6 % 2 gives 0, and then 9 % 0 causes a fatal error.
*Left-Sided Binding
Operators and Their Bindings: Exponentiation
print(2 ** 2 ** 3)
2 ** 2 → 4; 4 ** 3 → 64
PHYTON VARIABLE
a name;
a value (the content of the container)
If you want to give a name to a variable, you must follow some strict rules:
upper- and lower-case letters are treated as different (a little differently than
in the real world - Alice and ALICE are the same first names, but in Python they
are two different variable names, and consequently, two different variables);
the name of the variable must not be any of Python's reserved words (the keywords -
we'll explain more about this soon).
Shortcut Operators
For example, if we need to calculate a series of successive values of powers of 2,
we may use a piece like this:
x = x * 2
You may use an expression like this if you can't fall asleep and you're trying to
deal with it using some good, old-fashioned methods:
sheep = sheep + 1
Python offers you a shortened way of writing operations like these, which can be
coded as follows:
x *= 2
sheep += 1
a = 3.0
b = 4.0
print("c =", c)
anything = input()
• The program prompts the user to input some data from the console (most likely
using a keyboard, although it is also possible to input data using voice or image);
• the input() function is invoked without arguments the function will switch the
console to input mode; you'll see a blinking cursor, and you'll be able to input
some keystrokes, finishing off by hitting the Enter key; all the inputted data will
be sent to your program through the function's result;
• You need to assign the result to a variable; this is crucial - missing out this
step will cause the entered data to be lost;
Type CastingPython offers two simple functions to specify a type of data - here
they are: int() and float().
Their names are self-commenting:
the int() function takes one argument (e.g., a string: int(string)) and tries to
convert it into an integer; if it fails, the whole program will fail too (there is
a workaround for this situation, but we'll show you this a little later);
the float() function takes one argument (e.g., a string: float(string))and tries to
convert it into a float (the rest is the same).