-
Notifications
You must be signed in to change notification settings - Fork 4
Variables
Variables are containers for storing data values. variables are like labels for values. we can store a value and give it a name so that we can:
- refer back to it later
- use that value to do … stuff
- change it later on
score = 170
in above code, score
is variable and =
is assignment and 170
is value
Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they have been set.
x2 = 4 # x is of type int
x2 = "Sally" # x is now of type str
print(x2)
If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
You can get the data type of a variable with the type() function.
x3 = 5
y3 = "John"
print(type(x3))
print(type(y3))
Variable names are case-sensitive. This will create two variables:
a = 4
A = "Sally"
A
will not overwrite a
A variable can have a short name (like x and y) or a more descriptive name (age, car_name, total_volume). Rules for Python variables:
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (age, Age and AGE are three different variables).
- Don't use camelCase in variable names.
- Use underscore in names (pythonic).
- Don't use python keywords as variable names, see keywords using this command in interactive: help('keywords')
- use lowercase letters in variable names (PEP8)
Legal variable names
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable:
# Camel Case
myVariableName = "John"
# Pascal Case
MyVariableName = "John"
# Snake Case
my_variable_name = "John"
Python allows you to assign values to multiple variables in one line: Note: Make sure the number of variables matches the number of values, or else you will get an error.
x4, y4, z4 = "Orange", "Banana", "Cherry"
print(x4)
print(y4)
print(z4)
And you can assign the same value to multiple variables in one line:
x6 = y6 = z6 = "Orange"
print(x6)
print(y6)
print(z6)
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables, This is called unpacking.
# Unpack a list:
fruits = ["apple", "banana", "cherry"]
x5, y5, z5 = fruits
print(x5)
print(y5)
print(z5)
In the print() function, you output multiple variables, separated by a comma:
x6 = "Python"
y6 = "is"
z6 = "awesome"
print(x6, y6, z6)
- 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