-
Notifications
You must be signed in to change notification settings - Fork 4
Numbers
There are three numeric types in Python:
int
float
complex
. Variables of numeric types are created when you assign a value to them:
x = 1 # int
y = 2.8 # float
z = 1j # complex
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. they cant start with zero: 015
The
_
mean:31_000_000 = 31000000
. It's purely there for my sake, my human sake, not for python’s sake.
whole_number = (1)
whole_number2 = 1
whole_number3 = 11_05
positive_number = 11
negative_number = -11
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
float_number = 1.10
float_number2 = 1_500.55
float_number3 = 1.23e+15
float_number4 = 1.23 - 45
float_number5 = -.2
float_number6 = +2.1
float_number7 = 0.0
negative_float = -3.5
Float can also be scientific numbers with an
e
to indicate the power of 10.
float_number8 = 35e3
float_number9 = 87.7e100
Complex numbers are written with a "j" as the imaginary part:
complex_number = 3 + 5j
complex_number2 = 5j
complex_number3 = -5j
You can convert from one type to another with the
int()
,float()
, andcomplex()
methods:
Note: You cannot convert complex numbers into another number type.
Convert from one type to another:
int_num = 1 # int
float_num = 2.8 # float
complex_num = 1j # complex
# convert from int to float:
a = float(int_num)
# convert from float to int:
b = int(float_num)
# convert from int to complex:
c = complex(int_num)
Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers.
Import the random module, and display a random number between 1 and 9:
import random
print(random.randrange(1, 10))
- 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