-
Notifications
You must be signed in to change notification settings - Fork 4
Python literals
Python does allow, though, is the use of underscores in numeric literals.*
Therefore, you can write this number either like this: 11111111
, or like that: 11_111_111
And how do we code negative numbers in Python? As usual -
by adding a minus. You can write: -11111111
, or -11_111_111
Positive numbers do not need to be preceded by the plus sign, but it's permissible, if you wish to do it. The following lines describe the same number: +11111111
and 11111111
.
If an integer number is preceded by an 0O
or 0o
prefix (zero-o), it will be treated as an octal value. This means that the number must contain digits taken from the [0..7] range only.
0o123
is an octal number with a (decimal) value equal to 83
.
The print()
function does the conversion automatically. Try this:
print(0o123)
#83
The second convention allows us to use hexadecimal numbers. Such numbers should be preceded by the prefix 0x
or 0X
(zero-x).
0x123
is a hexadecimal number with a (decimal) value equal to 291
. The print() function can manage these values too. Try this:
print(0x123)
# 291
As you can probably imagine, the value of zero point four could be written in Python as:
0.4
But don't forget this simple rule - you can omit zero when it is the only digit in front of or after the decimal point.
In essence, you can write the value 0.4
as:
.4
For example: the value of 4.0
could be written as:
4.
This will change neither its type nor its value.
Take, for example, the speed of light, expressed in meters per second. Written directly it would look like this: 300000000
.
To avoid writing out so many zeros, physics textbooks use an abbreviated form, which you have probably already seen: 3 x 10^8
.
It reads: three times ten to the power of eight.
In Python, the same effect is achieved in a slightly different way - take a look:
3E8
The letter E
(you can also use the lower-case letter e
- it comes from the word exponent) is a concise record of the phrase times ten to the power of.
print(3E8)
# 300000000.0
print(0.0000000000000000000001)
# 1e-22
Python always chooses the more economical form of the number's presentation, and you should take this into consideration when creating literals.
Strings are used when you need to process text (like names of all kinds, addresses, novels, etc.), not numbers.
You already know a bit about them, e.g., that strings need quotes the way floats need points.
This is a very typical string: "I am a string."
print("I'm Monty Python.")
print('I like "Monty Python"')
As you can see, the backslash is a very powerful tool - it can escape not only quotes, but also apostrophes.
print("I like \"Monty Python\"")
print('I\'m Monty Python.')
A string can be empty - it may contain no characters at all. An empty string still remains a string:
''
""
in Python, True
and False
are literals representing the Boolean values:
print(True > False)
# True
print(True < False)
# False
...
-
Literals are 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."
). -
The binary system is a 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.
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 int ) are one of the numerical types supported by Python. They are numbers written without a fractional component, e.g.,
256
, or-1
(negative integers). -
Floating-point numbers (or simply float ) are another 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
. -
To encode an apostrophe or a quote inside a string you can either use the 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. -
Boolean values are the two constant objects
True
andFalse
used to represent truth values (in numeric contexts1
isTrue
, while0
isFalse
. -
There is one more, special literal that is used in Python: the
None
literal. This literal is a so-calledNoneType
object, and it is used to represent the absence of a value. We'll tell you more about it soon.
- Introduction
- Variables
- Data Types
- Numbers
- Casting
- Strings
- Booleans
- Operators
- Lists
- Tuple
- Sets
- Dictionaries
- Conditionals
- Loops
- Functions
- Lambda
- Classes
- Inheritance
- Iterators
- Multi‐Processing
- Multi‐Threading
- I/O Operations
- How can I check all the installed Python versions on Windows?
- Hello, world!
- Python literals
- Arithmetic operators and the hierarchy of priorities
- Variables
- Comments
- The input() function and string operators
Boolean values, conditional execution, loops, lists and list processing, logical and bitwise operations
- Comparison operators and conditional execution
- Loops
- [Logic and bit operations in Python]
- [Lists]
- [Sorting simple lists]
- [List processing]
- [Multidimensional arrays]
- Introduction
- Sorting Algorithms
- Search Algorithms
- Pattern-matching Algorithm
- Graph Algorithms
- Machine Learning Algorithms
- Encryption Algorithms
- Compression Algorithms
- Start a New Django Project
- Migration
- Start Server
- Requirements
- Other Commands
- Project Config
- Create Data Model
- Admin Panel
- Routing
- Views (Function Based)
- Views (Class Based)
- Django Template
- Model Managers and Querysets
- Form
- User model
- Authentification
- Send Email
- Flash messages
- Seed
- Organize Logic
- Django's Business Logic Services and Managers
- TestCase
- ASGI and WSGI
- Celery Framework
- Redis and Django
- Django Local Network Access
- Introduction
- API development
- API architecture
- lifecycle of APIs
- API Designing
- Implementing APIs
- Defining the API specification
- API Testing Tools
- API documentation
- API version
- REST APIs
- REST API URI naming rules
- Automated vs. Manual Testing
- Unit Tests vs. Integration Tests
- Choosing a Test Runner
- Writing Your First Test
- Executing Your First Test
- Testing for Django
- More Advanced Testing Scenarios
- Automating the Execution of Your Tests
- End-to-end
- Scenario
- Python Syntax
- Python OOP
- Python Developer position
- Python backend developer
- Clean Code
- Data Structures
- Algorithms
- Database
- PostgreSQL
- Redis
- Celery
- RabbitMQ
- Unit testing
- Web API
- REST API
- API documentation
- Django
- Django Advance
- Django ORM
- Django Models
- Django Views
- Django Rest Framework
- Django Rest Framework serializers
- Django Rest Framework views
- Django Rest Framework viewsets