Examples of variables in Python Programming


A variable is a named location used to store data in the memory

Example :

number = 10


All Contributions

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

How to use 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)

How to use string literals in Python :

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"

How to use Numeric literals in Python :

Example :

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)


Assigning multiple values to multiple variables :

Example :

a, b, c = 5, 3.2, "Hello"

print (a)
print (b)
print (c)

If we want to assign the same value to multiple variables at once, we can do this as:

x = y = z = "same"

print (x)
print (y)
print (z)

The second program assigns the same string to all the three variables x, y and z.

total contributions (7)

New to examplegeek.com?

Join us