Day: March 5, 2022

print function in python

print function is used to print a value, sentence and variable etc.
Let's look at some of the examples.
print('This is the statement that has to be printed')
output: Hello
        This is the statement that has to be printed
var1 = 10     #initializing a variable
print(var1)   #displaying the value stored in car1

var2 = 'M'    #initializing a variable
print(var2)   #displaying the value stored in var2

var3 = True   #initializing a variable
print(var3)   #displaying the value stored in var3
output: 10 

Other important operations using print function

Print a variable and it’s value together.

blog = ''
print('the name of the blog is', blog)
output: the name of the blog is
var1 = 10
var2 = 20
sum = var1 + var2
print('the sum', var1, '+', var2, 'is', sum) 
output: the sum 10 + 20 is 30

print function using format command

blog = ''
print(f'the blog name is {blog}')
output: the blog name is
Format command is used in print function, where in the key 'f' which is denoted as format and is used before the quotes. With the help of format command we can use the variable names directly within the flower brackets.

var2 = 10
var4 = 20 
sum = var2 + var4
print(f"the sum {var2} + {var4} is {sum}")
output: The sum  10 + 20 is 30
Read FullRead Full

Types of values that can be stored in python

In the previous post, we initialized variable with only numerical values.
Can we store a character, string and floating values in variable?
Yes we can store all kind of values in the variable.
Let's have a look how we can initialize all types of values.

How to initialize a character in python?

A value within quotes can be stored as type character.
variable1 = 'a' # character a is stored in variable1
variable2 = '1' # 1 is stored as a character in variable2
variable3 = '&' # special character is stored as character in variable3

How to initialize a string in python?

A word or a sentence within quotes can be stored as a string.
var1 = 'codicious'
var2 = 'this is how a string value is stored'

How to initialize a float in python?

float_1 = 15.256
float_2 = 365.029

How to initialize a variable with Boolean value in python?

There are two values in Boolean, i.e. True and False.
boolean_1 = True
boolean_2 = False
Read FullRead Full

Initializing variables and storing values in python

First let us know what is a value and variable?
A variable is something that stores specific value in it.
Now let's look into some basic examples how it works.
variable1 = 10 #line 1
variable2 = 3500 #line2
From the above example, 
value is 10, variable name is variable1. Value 10 is stored in variable1.
In the same way variable2 stores a value of 3500.

The name of a variable can be anything but starts with characters or _.
The code below is a simple way of initializing variables.
a1 = 10
_a = 10
The variable name cannot be started with numbers, special characters and blank spaces. The code gives syntax error as shown below.

Read FullRead Full