Variables, Literals, and Print Function¶
Introduction¶
In this blog, I will walk you through the print() function first, followed by variables, data types, and literals. I will explain about functions in some other dedicated blog on functions. However, knowledge of print() function is important in order to see the results. Without getting too much into introduction, let's start with our purpose and uncover the beauty of python on the go.
What is a print() function in python?¶
print() function is python's built-in function to display texts, variables, values, or other types of data output on screen or console. The usage of print() function is an easy and convenient way to interact with users to show them information during the program execution. Using print() is a common way of debugging code as well.
The basic syntax of the print() function is as follows:
print(val1, val2, …, sep=' ', end='\n')
The content inside the brackets () of print function are known as arguments and they serve the following role -
- val1, val2, … : There are the values or expressions to display as an output on console. There can be multiple values separated by commas depending on the programming need. print() can display the values directly or can display values stored in variables. If you want to print values directly then string types has to be passed in quotations and numbers can be printed as such. For example,
print('Hi! I am a string and am inside quotations')
Hi! I am a string and am inside quotations
print(102)
102
variable_storing_value = 'You can store a string or number here'
print(variable_storing_value)
You can store a string or number here
print() function needs to have at least one value to show some output on screen. If no value is passed to print() function then it will print a single new line.
- 'sep' - this is an argument and is optional. This is used for specifying separator between different values provided in print function. By default, it uses a space, but we can change the default value. Let's see this in code -
print(10, 20, 30, 40 ,50)
10 20 30 40 50
print(10, 20, 30, 40 ,50 , sep = '_')
10_20_30_40_50
print(10, 20, 30, 40 ,50 , sep = '_can be anything_')
10_can be anything_20_can be anything_30_can be anything_40_can be anything_50
- 'end=': this is an argument and is optional. This defines what to print at the end of the print function call. By default, it prints a newline character but this can be changed any other string or a space to prevent newline. For example,
print('This is the first print function. ')
print('This is the second print function.')
This is the first print function. This is the second print function.
print('This is the first print function. ', end = ' ')
print('This is the second print function.')
This is the first print function. This is the second print function.
What are Variables in python?¶
Variables in python are like containers, which stores data or values in order to be accessed, modified, and used in various parts of a program. This container (variable) can hol any data type such as numbers, strings, boolean etc. Python does not require explicit declaration of variable data types. It automatically assigns a data type to the variable as per the value stored in it. Values are assigned (or data is stored) in a variable using the assignment operator ('='). Strings should be inside quotations and numbers could be stored as it is.
Basic syntax is - variable_name = value.
Let's see few examples -
my_name = 'Nishi Paul'
print(my_name)
Nishi Paul
number_storage = 10
print(number_storage)
10
am_i_a_student = True #once a student, always a student. By the way I have used commenting here
'''
You can comment or write anything using a # and is commonly used for defining the code.
Using # you can write comment in a single line. For multiline commenting, \''' is used, like the way I am using here :)
'''
print(am_i_a_student)
True
temperature_today = 25.8
print('This is an example of floating number - ',temperature_today)
This is an example of floating number - 25.8
Variables must be declared keeping following rules in mind -
- They can only contain letters (a-z, A-Z), digits (0–9), and underscores (_).
- They cannot start with a digit.
- Variable names are case-sensitive (for ex, a and A are different variables).
- It is essential to choose meaningful and descriptive names for variables to make your code more readable and maintainable.
What are Data Types in python?¶
Now that we know about variables, let's walk through data types a bit. As we have already mentioned that variables are like containers which holds data and each data has a certain type. This means data can be integer, string etc. We will see different types of data types that are automatically determined by python, which means we do not have to mention anything explicitly prior to storing the data in a variable. Some of the data types in python are -
- Numeric Types:
- Integer (int): Represents whole numbers (can be positive or negative). For ex, 1, 10, -5.
- Floating Point (float): Represents decimal numbers (can be positive or negative). For ex, 3.14, -0.5, 2.0.
- Complex (complex): Represents complex numbers in the form of a + bj, where a and b are real numbers, and j represents the imaginary unit (For ex, 3 + 4j).
interger_variable_positive, interger_variable_negative = 15, -45
print('Yes! You can store values side-by-side.',interger_variable_positive, interger_variable_negative, sep='\n')
Yes! You can store values side-by-side. 15 -45
float_variable_positive, float_variable_negative = 15.365478, -45.3546971
print('Yes! Separators can be given as newline characters too!', float_variable_positive, float_variable_negative, sep='\n')
Yes! Separators can be given as newline characters too! 15.365478 -45.3546971
complex_number = (2+7j) + (3+2j)
print('To know more about numbers, please follow documentation. \
Our result for the addition of the complex numbers is - ',complex_number)
To know more about numbers, please follow documentation. Our result for the addition of the complex numbers is - (5+9j)
- Text Type:
- String (str): Represents a sequence of characters and enclosed in single (' '), double (" "), or triple (''' ''' or """ """) quotes, For ex, 'Hello', "Python". Thus in python triple quotes can be used for multi-line commenting and multi-line string entry both.
name = 'Nishi Paul'
city = "Kolkata"
bio = '''I am a Data Scientist professional and student both.
As I believe, we are always a student.'''
print('Hi my name is ',name, ' and I live in ', city,' .',bio)
Hi my name is Nishi Paul and I live in Kolkata . I am a Data Scientist professional and student both. As I believe, we are always a student.
- Boolean Type:
- Boolean (bool): Represents the truth values True or False. It is used in logical expressions and conditional statements.
is_correct = True
print('Boolean Types are mostly used in conditional statements that we will see next - ', is_correct)
Boolean Types are mostly used in conditional statements that we will see next - True
- Sequence Types:
- List (list): Represents ordered collections of items, enclosed in square brackets. Lists are mutable, meaning you can change their elements after creation.
- Tuple (tuple): Represents ordered collections of items, enclosed in parentheses. Tuples are immutable, meaning their elements cannot be changed after creation.
- Range (range): Represents a sequence of numbers and is often used for creating loops.
- Set Types:
- Set (set): Represents an unordered collection of unique elements, enclosed in curly braces. Sets do not allow duplicate values.
- FrozenSet (frozenset): Represents an immutable set, meaning its elements cannot be modified after creation.
- Mapping Type:
- Dictionary (dict): Represents a collection of key-value pairs, enclosed in curly braces. Dictionaries allow fast look-up of values based on their keys.
- None Type:
- None (NoneType): Represents the absence of a value. It is often used as a default value for variables that have not been assigned.
What are Literals in python?¶
In Python, literal is data whose values are determined by the literal itself. They are used to assign values to variables or to represent values in expressions. Python supports various types of literals, each representing a specific data type.
Here are some common types of literals in Python:
Numeric Literals: These represent numeric values, such as integers, floating-point numbers, and complex numbers. Examples include 42, 3.14, and 2 + 3j.
String Literals: These represent sequences of characters and are enclosed in single (' '), double (" "), or triple (''' ''' or """ """) quotes. Examples include 'Hello', "Python", and '''Multi-line string'''.
Boolean Literals: These represent the truth values 'True' and 'False' and are used in logical expressions.
None Literal: The None
literal represents a special value that denotes the absence of a value. It is often used as a placeholder or to indicate a variable with no value.
Python's support for literals makes it convenient to work with various data types and helps improve code readability.
Conclusion¶
Python is an easy to use, interpreted, and high level abstraction language which enables dynamic data typing and is platform independent. In this blog, we have gone through the very basics yet several important terms. We will be making use of this knowledge extensively in designing code flows.