What are Numbers in Python?
Types of numbersPython has various
"types" of numbers (numeric literals). We'll mainly focus on integers
and floating-point numbers.
Integers are just whole
numbers, positive or negative. For example, 2 and -2 are examples of integers.
Floating-point numbers in
Python is notable because they have a decimal point in them, or use an
exponential (e) to define the number. For example, 2.0 and -2.1 are examples of
floating-point numbers. 4E2 (4 times 10 to the power of 2) is also an example
of a floating-point number in Python.
Throughout this course we
will be mainly working with integers or simple float number types.
Here is a table of the two
main types we will spend most of our time working with some examples:
1 , 2 , -5 , 1000 Integers
1.2 , -0.5 , 2e2
, 3E2 Floating-point numbers
Now that we've seen how to use numbers in Python as a calculator let's see how we can assign names and create variables. We use a single equals sign to assign labels to variables. Let's see a few examples of how we can do this
The names you use when creating these labels need to follow a few rules:
1. Names can not start with a number.
2. There can be no spaces in the name, use _ instead.
3. Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+
4. It's considered best practice (PEP8) that names are lowercase.
5. Avoid using the characters 'l' (lowercase letter el), 'O' (uppercase letter oh),
or 'I' (uppercase letter eye) as single character variable names.
6. Avoid using words that have special meaning in Python like "list" and "str"Using variable names can be a very useful way to keep track of different variables in Python.
Determining variable type with type()
- You can check what type of object is assigned to a variable using Python's built-in
type()function. Common data types include:
- int (for integer)
- float
- str (for string)
- list
- tuple
- dict (for dictionary)
- set
- bool (for Boolean True/False)

Comments
Post a Comment