Module Weeks 4 5
Module Weeks 4 5
Most Essential Learning Know how to use variables, constants, and literals are data that they will used in
Competencies (MELCs) programming, Work with data types and different type conversion, Realize that
knowing the type of fata that they will be using can be a very useful tool in
programming
REFERENCES: (Please be guided with the given references to help you perform the given activities. Click the given links and
hyperlinks to access the suggested learning resources.)
Printed: Cobre, E. J., Olalia, N., & Tingzon, K. (2019). ICT Tools Today High School Series Computers for Digital
Learners Grade 10 (1st ed., Vol. 1) [Book]. The Phoenix Publishing house Inc.
CONTENT DISCUSSION:
Python Variables
In programming, a variable is a container (storage area) to hold data. For example,
number = 10
print(site_name)
# Output: programiz.pro
Output
apple.com
In the above example, we assigned the value programiz.pro to the site_name variable. Then, we printed out the value assigned
to site_name
print(site_name)
Output
programiz.pro
apple.com
print(a) # prints 5
print(b) # prints 3.2
print(c) # prints Hello
If we want to assign the same value to multiple variables at once, we can do this as:
site1 = site2 = 'programiz.com'
snake_case
MACRO_CASE
camelCase
CapWords
Create a name that makes sense. For example, vowel makes more sense than v.
If you want to create a variable name having two words, use underscore to separate them. For example:
my_name
current_salary
var num = 5
var Num = 55
print(num) # 5
print(Num) # 55
Python Constants
A constant is a special type of variable whose value cannot be changed.
In Python, constants are usually declared and assigned in a module (a new file containing variables, functions, etc which is imported to
the main file).
Let's see how we declare constants in separate file and use it in the main file,
Create a constant.py:
# declare constants
PI = 3.14
GRAVITY = 9.8
Create a main.py:
In the above example, we created the constant.py module file. Then, we assigned the constant value to PI and GRAVITY.
After that, we create the main.py file and import the constant module. Finally, we printed the constant value.
Python Literals
Literals are representations of fixed values in a program. They can be numbers, characters, or strings, etc. For example, 'Hello,
World!', 12, 23.0, 'C', etc.
Literals are often used to assign values to variables or constants. For example,
site_name = 'programiz.com'
0b101,
Binary Start with 0b .
0b11
Floating-point
10.5, 3.14 Containing floating decimal points.
Literal
pass = true
some_character = 'S'
print(value)
# Output: None
Here, we get None as an output as the value variable has no value assigned to it.
Literal Collections
There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.
# list literal
fruits = ["apple", "mango", "orange"]
print(fruits)
# tuple literal
numbers = (1, 2, 3)
print(numbers)
# dictionary literal
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'}
print(alphabets)
# set literal
vowels = {'a', 'e', 'i' , 'o', 'u'}
print(vowels)
Output
In the above example, we created a list of fruits, a tuple of numbers, a dictionary of alphabets having values with keys designated to
each value and a set of vowels.
Variable is a named location used to store data in the memory. It has a unique name called identifier. We use the equal (=) sign to
assign the value to a variable.
A constant is a variable whose values do not change. It is usually assigned and declared on a module. A module is a new file that
contains values or functions, which is imported to the main file. They are written in all capital letters and underscores to separate the
words.
Literal is a raw data given in a variable constant.
Numeric literals are unchangeable. The four (34) numerical types are: plain, integers, long integers, floating point numbers, and
imaginary numbers.
A string literal is a sequence of characters surrounded by single, double, or triple quotes.
A character literal is a single character surrounded by single or double quotes.
In computer programming, data types specify the type of data that can be stored inside a variable. For example,
num = 24
Since everything is an object in Python programming, data types are actually classes and variables are instances(object) of these
classes.
Integers, floating-point numbers and complex numbers fall under Python numbers category. They are defined
as int, float and complex classes in Python.
We can use the type() function to know which class a variable or a value belongs to.
num1 = 5
print(num1, 'is of type', type(num1))
num2 = 2.0
print(num2, 'is of type', type(num2))
num3 = 1+2j
print(num3, 'is of type', type(num3))
Run Code
Output
In the above example, we have created three variables named num1, num2 and num3 with values 5, 5.0, and 1+2j respectively.
We have also used the type() function to know which class a certain variable belongs to.
Since,
5 is an integer value, type() returns int as the class of num1 i.e <class 'int'>
2.0 is a floating value, type() returns float as the class of num2 i.e <class 'float'>
1 + 2j is a complex number, type() returns complex as the class of num3 i.e <class 'complex'>
Python List Data Type
List is an ordered collection of similar or different types of items separated by commas and enclosed within brackets [ ]. For example,
To access items from a list, we use the index number (0, 1, 2 ...). For example,
In the above example, we have used the index values to access items from the languages list.
languages[0] - access first item from languages i.e. Swift
languages[2] - access third item from languages i.e. Python
Similar to lists, we use the index number to access tuple items in Python .
For example,
# create a tuple
product = ('Microsoft', 'Xbox', 499.99)
String is a sequence of characters represented by either single or double quotes. For example,
name = 'Python'
print(name)
Python
Python for beginners
Set is an unordered collection of unique items. Set is defined by values separated by commas inside braces { }.
For example,
# create a set named student_id
student_id = {112, 114, 116, 118, 115}
Output
In programming, type conversion is the process of converting data of one type to another. For example: converting integer data
to string.
There are two types of type conversion in Python.
Implicit Conversion - automatic type conversion
Explicit Conversion - manual type conversion
Output
Value: 124.23
Data Type: <class 'float'>
Note:
We get TypeError, if we try to add string and integer. For example, '12' + 23. Python is not able to use Implicit Conversion in
such conditions.
Python has a solution for these types of situations which is known as Explicit Conversion.
In Explicit Type Conversion, users convert the data type of an object to required data type.
We use the built-in functions like int(), float(), str(), etc to perform explicit type conversion.
This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
print("Sum:",num_sum)
print("Data type of num_sum:",type(num_sum))
Output
num_string = int(num_string)
Here, we have used int() to perform explicit type conversion of num_string to integer type.
After converting num_string to an integer value, Python is able to add these two variables.
Finally, we got the num_sum value i.e 35 and data type to be int.