Contents
Python Variables
A Python variables is a designated spot in memory where data is kept. Consider variables as a storage space for data that may be altered later in the program. This is a useful way to conceive of variables. For example,
number = 10
Here, a variable with the name number has been made. The variable has been given the value of 10.
Variables may be compared to a bag used to contain books that can be changed at any moment.
number = 10 number = 1.1
The value of the number was initially 10. It was afterward updated to 1.1.
Note: In Python, we don’t really give the values of the variables. Python instead assigns the variable a reference to the object(value).
Assigning values to Python variables
As you can see from the example above, you can give a variable a value by using the assignment operator “=”.
Example 1: Declaring a Python variables and giving it a value
website = "apple.com" print(website)
Output
apple.com
The variable website was given the value apple.com in the program mentioned above. The value allocated to the website, apple.com, was then printed out.
Note: Because Python is a type-inferred language, the variable type does not need to be declared explicitly. It declares the website variable as a string since it instantly recognises that apple.com is a string.
Example 2: Modifying a Python variable‘s value
website = "apple.com" print(website) # assigning a new value to website website = "programiz.com" print(website)
Output
apple.com programiz.com
Apple.com has been first allocated to the website variable in the program above. Then programiz.com is added as the value.
Example 3: Giving different values to various Python variables
a, b, c = 5, 3.2, "Hello" print (a) print (b) print (c)
The following are the possible ways to simultaneously assign the same value to many variables:
x = y = z = "same" print (x) print (y) print (z)
In the second program, the three variables x, y, and z are all given the identical string.
Constants
A variable type whose value cannot be altered is a constant. Constants may be thought of as storage spaces for data that cannot be altered in the future.
Constants can be compared to a bag used to keep books that, once placed within the bag, cannot be replaced.
Giving a constant in Python a value
Constants are typically declared and allocated in a module in Python. The module in this case is a new file that is imported into the main file and contains variables, functions, etc. Constants are written inside the module using all capital letters with an underscore between each word.
Example 3: Declaring and giving a constant a value
Create a constant.py
PI = 3.14 GRAVITY = 9.8
Create a main.py
import constant print(constant.PI) print(constant.GRAVITY)
Output
3.14 9.8
A constant.py module file is created in the application mentioned above. Then, we give PI and GRAVITY the constant value. The constant module is then imported into the main.py file. Finally, the constant value is printed.
Note: Python doesn’t really utilize constants. It is customary to name them in full capital letters to distinguish them from variables, but this does not really stop reassignment.
Rules and Constant Naming Convention for Variables
- Names for variables and constants should be composed of a mix of lowercase (a to z) and uppercase (A to Z) characters, numerals (0 to 9), and underscores ( ). For example:
snake_case MACRO_CASE camelCase CapWords
- Make a logical name for your product. Vowel, for instance, makes more sense than v.
- Use the underscore character to divide two words into distinct words in a variable name. For example:
my_name current_salary
- To declare a constant, use capital letters whenever feasible. For example:
PI G MASS SPEED_OF_LIGHT TEMP
- Never use any special characters, such as! @, #, $, %, etc.
- A variable name shouldn’t begin with a number.
Literals
A literal is a set of unprocessed data in a variable or constant. There are several different kinds of literal in Python, including the following:
Numeric literals
Number Literals are unchangeable (unchangeable). Integer, Float, and Complex are the three numerical kinds to which numerical literals can belong.
Example 4: How to utilize Python’s numeric literals?
a = 0b1010 #Binary Literals b = 100 #Decimal Literal c = 0o310 #Octal Literal d = 0x12c #Hexadecimal Literal #Float Literal float_1 = 10.5 float_2 = 1.5e2 #Complex Literal x = 3.14j print(a, b, c, d) print(float_1, float_2) print(x, x.imag, x.real)
Output
10 100 200 300 10.5 150.0 3.14j 3.14 0.0
In the program mentioned above
We allocated separate variables with integer literals. Here, the letters a, b, c, and d are literals in binary, decimal, octal, and hexadecimal respectively.
All of the literal values are changed to decimal values before printing the variables.
We gave the variable x a complex literal, 3.14j. Then, we build the imaginary and real components of complex numbers using the imaginary literal (x.imag) and the real literal (x.real).
String literals(Python variables)
A series of characters enclosed in quotations is referred to as a string literal. For a string, we can use single, double, or triple quotations. Additionally, a literal character is a single character enclosed in a single or double quote.
Example 7: How do you use Python’s string literals?(Python variables)
strings = "This is Python" char = "C" multiline_str = """This is a multiline string with more than one line code.""" unicode = u"\u00dcnic\u00f6de" raw_str = r"raw \n string" print(strings) print(char) print(multiline_str) print(unicode) print(raw_str)
Output
This is Python C This is a multiline string with more than one line code. Ünicöde raw \n string
This is Python is a string literal and C is a character literal in the program above.
A multi-line string literal is the value enclosed in triple quotes “”” that was assigned to the multiline str.
A Unicode literal that supports characters other than English is the string u”u00dcnicu00f6de.” In this case, u00dc stands in for Ü and u00f6 for ö.
A raw string literal is r”raw n string”.
Boolean literals(Python variables)
A Boolean literal can be True or False, one of the two possible values.
Example 8: How to utilize boolean literals in Python.
x = (1 == True) y = (1 == False) a = True + 4 b = False + 10 print("x is", x) print("y is", y) print("a:", a) print("b:", b)
Output
x is True y is False a: 5 b: 10
We use the boolean literals True and False in the program above. Python displays the value as 1 for True and 0 for False. Since 1 is equivalent to True, the value of x is True. Additionally, since 1 does not equal False, the value of y is False.
Similar to this, True and False can be used as values in numerical expressions. Because we multiply True, which has a value of 1, by 4, the value of a becomes 5. Similar to a, b is 10 as a result of adding a False with a value of 0 to 10.
Special literals
Python has one unique literal, called None. It’s what we use to indicate that the field hasn’t been made.
Example 9: How to use special literals in Python variables?
drink = "Available" food = None def menu(x): if x == drink: print(drink) else: print(food) menu(drink) menu(food)
Output
Available None
We define a menu function in the software mentioned above. When we select a drink as the argument inside the menu, Available appears. And it shows None when the argument is food.
Literal Collections
Four separate literal sets are available. List, triple, dictionary, and set literals are examples of literals.
Example 10: How to use literals collections in Python?
fruits = ["apple", "mango", "orange"] #list numbers = (1, 2, 3) #tuple alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary vowels = {'a', 'e', 'i' , 'o', 'u'} #set print(fruits) print(numbers) print(alphabets) print(vowels)
Output
['apple', 'mango', 'orange'] (1, 2, 3) {'a': 'apple', 'b': 'ball', 'c': 'cat'} {'e', 'a', 'o', 'i', 'u'}
A list of fruits, a tuple of numbers, a dictionary dict with values and keys assigned to each value, a set of vowels, and a list of numbers were all produced in the aforementioned application.