Initialising variables in Python: Numeric Types, String and Boolean
This post covers some methods of initialising Numeric type (int, float, complex), String and Boolean data type variables in python.
This notebook is available here: https://github.com/AdityaGarg1995/BeginnerPython
Initialising Numeric Variables
Initialising Integer Variables
## Initialising integers
### Assigning a value explicitly
x = 9 ## Initialises x as integer with value = 9
y = -9 ## Initialises y as integer with value = -9
### Without assigning a value explicitly
z = int() ## Initialises z as integer with value = 0
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))Initialising Float variables
## Initialising floats
### Assigning a value explicitly
x = 1.5 ## Initialises x as float with value = 1.5
y = -1.5 ## Initialises y as float with value = -1.5
### Without assigning a value explicitly
z = float() ## Initialises z as float with value = 0.0
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))Initialising Complex Number Variables
Reference: https://docs.python.org/3/library/functions.html#complex
## Initialise complex number
### Initialise complex number by explicitly providing real and imaginary values
x = complex (1, 5)
y = complex (-1.5, 9.7)
z = complex (6.889, -9.006)
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))### Initialise complex number using other variables for real and imaginary values
a = 1.5
b = 9.7
x = complex(a,b)
y = complex (-a, b)
z = complex (a, -b)
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))### Initialise complex number using string input
x = complex('1+2j')
y = complex('1-2j')
z = complex('-1+2j')
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))Initialising String Variables
Strings are immutable sequences of Unicode code points. String literals are written in a variety of ways:
- Single quotes:
'allows embedded "double" quotes' - Double quotes:
"allows embedded 'single' quotes" - Triple quoted:
'''Three single quotes''',"""Three double quotes"""→ Triple quoted strings may span multiple lines — all associated whitespace will be included in the string literal.
Initialising non-empty single line string variables
x = 'Hello World' ## Initialises with single quotes
y = "Hello World" ## Initialises with double quotes
v = str('Hello World') ## Initialise using constructor with single quotes
w = str("Hello World") ## Initialise using constructor with double quotes
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of v = ')
print(v)
print('Data type of v is: ')
print(type(v))
print('Value of w = ')
print(w)
print('Data type of w is: ')
print(type(w))Initialising Multi-line String Variables
Get Aditya Garg’s stories in your inbox
Join Medium for free to get updates from this writer.
Initialising by simply putting string in multiple lines will throw error
Initialising multi-line string variables is achieved using triple quoting: use three pairs of single or double quotes to enclose the string as described below.
## Initialise multi-line string variables --> Needs triple quoting
## Initialises multi-line string variable by triple quoting using single quotes
x = '''Hello
World'''
## Initialises multi-line string variable by triple quoting using single quotes
y = """Hello
World"""
## Initialises multi-line string variable by triple quoting using single quotes
v = str('''Hello
World''')
## Initialises multi-line string variable by triple quoting using single quotes
w = str("""Hello
World""")
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of v = ')
print(v)
print('Data type of v is: ')
print(type(v))
print('Value of w = ')
print(w)
print('Data type of w is: ')
print(type(w))Initialising Empty String Variable
## Initialise empty string
x = '' ## Initialises with single quotes
y = "" ## Initialises with double quotes
z = str() ## Initialises using constructor
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))Initialising Boolean Variables
Boolean variables can be initialised using the keywords True and False.
Additionally, all non-zero numeric types and non-empty strings evaluate to True if given as bool() constructor input.
## Intialising Boolean variables using True and False keywords
x = True
y = False
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))## All non-zero numeric types and non-empty strings evaluate to True if given as bool() constructor input
### Initialising boolean using Numeric inputs to constructor
x = bool(20)
y = bool(-30)
z = bool(0)
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))## All non-zero numeric types and non-empty strings evaluate to True if given as bool() constructor input
### Initialising boolean using Complex Number inputs to constructor
x = bool(complex(20, 30))
y = bool(complex(-10, -30))
z = bool(complex(0,0))
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))## All non-zero numeric types and non-empty strings evaluate to True if given as bool() constructor input
### Initialising boolean using String inputs to constructor
x = bool('Hello')
y = bool("Hello")
z = bool('')
print('Value of x = ')
print(x)
print('Data type of x is: ')
print(type(x))
print('Value of y = ')
print(y)
print('Data type of y is: ')
print(type(y))
print('Value of z = ')
print(z)
print('Data type of z is: ')
print(type(z))Disallowed Variable Names
Variable names cannot start with a number — if so initialised, an error is thrown, as in below example.









