Variables in Python - Practical Examples of Python Variable
- Variables In Python
- Changing the Value of a Variable in Python Programming
- Constants In python
- String and Character Literals in Python Program
¶Variables In Python
In Python programming, a variable is a container (storage area) to hold data. Let’s See For example,
number = 10
Above, number
is the variable storing the value 10.
¶Assigning values to Python Variables
As like we can see from the upon example, we can use the assignment operator =
to assign a value to a variable.
# assign value to site_name variable
site_name = 'devsenv.pro'
print(site_name)
# Output: devsenv.pro
Now in the upon example, we assigned the value'devsenv.pro'
to the site_name
variable. After, we printed out the value assigned to site_name
.
¶Changing the Value of a Variable in Python Programming
site_name = 'devsenv.pro'
print(site_name)
# assigning a new value to site_name
site_name = 'akash.com'
print(site_name)
Output :
devsenv.pro'
akash.com
Above, the value of site_name
is changed from 'devsenv.pro'
to 'akash.com'
.
¶Example: Assigning multiple values to multiple variables In python
a, b, c = 5, 3.2, 'Hello'
print(a) # prints 5
print(b) # prints 3.2
print(c) # prints Hello
If you want to assign the same value to multiple variables at once again, we can do this as below-
site1 = site2 = 'programiz.com'
print(site1) # prints devsenv.com
print(site2) # prints devsenv.com
Below, you have assigned the same string value 'devsenv.com'
to both the variables site1
& site2
.
¶Rules for Naming Variables In Python
- Constant and variable names should have a abbreviation of letters in lowercase (a to z) or uppercase** (A to Z) **or digits (0 to 9) or an underscore
(_)
. For example Below:
snake_case
MACRO_CASE
camelCase
CapWords
- Create a name that be aware. For example,
vowel
makes more be aware thanv
. - Suppose you want to create a variable name having two words, use underscore to several them. For example below:
my_name
current_salary
- Python is a case-sensitive. So
num
andNum
are several variables. For example below,
var num = 5
var Num = 55
print(num) # 5
print(Num) # 55
- Eliminate using keywords like
if
,True
,class
, etc. as like variable names.
¶Constants In python
A certain is a special type of variable whose value cannot be changed.
In Python Program, certain are generally declared and assigned in a module (a new file containing variables, functions, etc which is imported to the main file).
Now let’s see how we declare constants in several file and use it in the main file.
Create a constant.py:
# declare constants
PI = 3.14
GRAVITY = 9.8
Create a main.py:
# import constant file we created above
import constant
print(constant.PI) # prints 3.14
print(constant.GRAVITY) # prints 9.8
In the upon example, we maked the constant.py module file. After, we assigned the constant value to PI
and GRAVITY
.
After this, we make the main.py file and import the constant
module. Finally, you printed the constant value.
¶Literals In Python
Literals are deputation of fixed values in a program. Which can be numbers, characters, or strings, etc. For example, 'Hello, World!'
, 12
, 23.0
, ’C'
, etc.
Literals are often to used to assign values to variables or constants. For example below-
site_name = 'devsenv.com'
In the upon example, site_name
is a variable, and 'devsenv.com'
is a literal.
¶Numeric Literals In python
Numeric Literals are unchangeable). Numeric literals can belong to 3 several numerical types: Integer
, Float
, and Complex
.
¶Boolean Literals In python
We see there are two boolean literals: True
and False
.
Let’s see for example-
result1 = True
Above, True
is a boolean literal assigned to result1
.
¶String and Character Literals in Python Program
Character literals are unicode characters surrounded in a quote. For example-
some_character = 'S'
Above, S
is a character literal assigned to some_character
.
Likewise, String literals are series of Characters enclosed in quotation marks.
For Example-
some_string = 'Python is fun'
Above, 'Python is fun'
is a string literal assigned to some_string
.
¶Special Literal in Python Program
Python takes on one special literal None
. We can use it to specify a null variable. For example below-
value = None
print(value)
# Output: None
Above, we will show None
as an output as the value
variable has no value assigned to it.
¶Literal Collections In python
There are four several literal collections List literals, Tuple literals, Dict literals, and Set literals.
# list literal
fruits = ["apple", "mango", "orange"]
print(fruits)
# tuple literal
numbers = (1, 2, 3)
print(numbers)
# dictionary literal
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'}
print(alphabets)
# set literal
vowels = {'a', 'e', 'i' , 'o', 'u'}
print(vowels)
Output :
['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}
In the upon example, we maked a list of fruits
, a tuple of numbers
, a dictionary of alphabets
having values with keys designated to every value and a set of vowels.
Hello World In Python - First Programming Example in Python
Python Functions Mastery Your Complete Guide for Success
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development