<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged variables</title>
<link>https://coderlegion.com/tag/variables</link>
<description>Powered by Question2Answer</description>
<item>
<title>Python Variables and Data Types</title>
<link>https://coderlegion.com/201/python-variables-and-data-types</link>
<description>&lt;h2&gt;A. Introduction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Variables are containers&lt;/strong&gt; of values or data that allow the programmers to manipulate the value indirectly. Variable values can be changed, they can be useful for tracking the state of certain objects. By giving &lt;strong&gt;meaningful variable naming&lt;/strong&gt;, it can improve code readability. In a way variables can make complicated concepts clearer. Imagine we are reading a book on trigonometry, certainly we would see the formula to get the area of a rectangle, the perimeter of a circle, etc.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Data types&lt;/strong&gt; are the classification of objects such as integer, string, float, boolean, list, dictionary, etc. They are classified to improve the performance of Python, not just Python but for other programming languages. In Python, data is commonly assigned to a variable as part of the language &lt;a rel=&quot;nofollow&quot; href=&quot;https://logiclair.org/1761/python-basics-python-syntax&quot;&gt;syntax&lt;/a&gt; to enhance code abstraction and readability within the program or application.&lt;/p&gt;
&lt;h2&gt;B. Variables&lt;/h2&gt;
&lt;h3&gt;1. Definition and Creation&lt;/h3&gt;
&lt;p&gt;Let us say we will store the age of a tree in a variable.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;treeage = 5
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;treeage&lt;/code&gt; is our variable name while the &lt;code&gt;5&lt;/code&gt; is our data or value. The variable name must be at the left-hand side of the equal sign.&lt;/p&gt;
&lt;p&gt;In other languages such as &lt;code&gt;C/c++&lt;/code&gt; we need to define the type.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int treeage = 5;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Python we don't need it. However you can do so by using typing.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;treeage: int = 5
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That &lt;code&gt;int&lt;/code&gt; is a python standard data type.&lt;/p&gt;
&lt;h3&gt;2. Naming Conventions&lt;/h3&gt;
&lt;p&gt;It is very important to know how to name a variable.&lt;/p&gt;
&lt;h4&gt;a. A variable name must be one word or connected with underscore if more than one&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;incorrect&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;tree age = 5
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;correct&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;treeage = 5
tree_age = 5
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;b. The variable name is case sensitive.&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;Treeage&lt;/code&gt; is different from &lt;code&gt;treeage&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;c. The variable name must not start with an integer.&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;incorrect&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;4mycar = 4
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;correct&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my4car = 4
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-blue&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Integers in a variable are allowed but not as the first character.&lt;/span&gt;
&lt;/div&gt;
&lt;h4&gt;d. The variable name must not be a keyword&lt;/h4&gt;
&lt;p&gt;Python has reserved words or keywords that it used internally. They should not be used as a variable name.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;incorrect&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for = &quot;mango&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is not allowed because &lt;code&gt;for&lt;/code&gt; is a keyword in Python.&lt;/p&gt;
&lt;p&gt;What are the keywords in python?&lt;/p&gt;
&lt;p&gt;We can generate Python's keywords using the following code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import keyword

python_keywords = keyword.kwlist
print(python_keywords)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-red&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Caution:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Do not use Python's keyword as a variable name.&lt;/span&gt;
&lt;/div&gt;
&lt;h3&gt;3. Variable Scope&lt;/h3&gt;
&lt;p&gt;There are two basic scopes of a variable they are local and global. Global variables are those whose values can be access anywhere in the code. Local variables are limited to a block where it is defined.&lt;/p&gt;
&lt;p&gt;Here is an example of a global and local variables.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;global_local_variables.py&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;num_ships = 10  # global
print(f'initial value of global variable num_ships: {num_ships}')

def get_num_ships():
    # Use the global variable inside the function.
    global num_ships

    local_num_ships = 4  # local
    print(f'local variable num_ships: {local_num_ships}')

    # Access the global variable inside the function.
    print(f'global variable num_ships inside a function: {num_ships}')

    # Modify the value of global variable.    
    num_ships = 20
    print(f'modified global variable num_ships inside a function: {num_ships}')

get_num_ships()
print(f'global variable num_ships outside the function: {num_ships}')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python global_local_variables.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;initial value of global variable num_ships: 10
local variable num_ships: 4
global variable num_ships inside a function: 10
modified global variable num_ships inside a function: 20
global variable num_ships outside the function: 20
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;C. Data Types&lt;/h2&gt;
&lt;p&gt;Data types are just categories of values such as integer, float or string, etc. This classification is useful to Python to efficiently perform its operations. Each of this data type has its own limit of what operators it can be applied upon. Consider an example, can we divide the name of the planet as &lt;code&gt;str&lt;/code&gt; by a number as &lt;code&gt;int&lt;/code&gt;? No. Only an integer or float can only be divided by an integer or float. By classifying values Python can easily determine that a string divided by an integer would result to an exception or error.&lt;/p&gt;
&lt;h3&gt;1. Data Types Overview&lt;/h3&gt;
&lt;p&gt;Here is an overview of Python's standard data types.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=3477276320270304849&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Let me show you how each of them will be used. I will use Python's REPL (Read Eval Print Loop) environment.&lt;/p&gt;
&lt;p&gt;To start the REPL you can open your terminal (I will use powershell) and type python.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=15242165855921458557&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h4&gt;a. Integer&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex&quot;&gt;Integer&lt;/a&gt; or &lt;code&gt;int&lt;/code&gt; is a whole number that can be negative, zero or positive.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; num_employees = 100
&amp;gt;&amp;gt;&amp;gt; isinstance(num_employees, int)
True
&amp;gt;&amp;gt;&amp;gt; num_brownouts = 0
&amp;gt;&amp;gt;&amp;gt; isinstance(num_brownouts, int)
True
&amp;gt;&amp;gt;&amp;gt; error_code = -1
&amp;gt;&amp;gt;&amp;gt; isinstance(error_code, int)
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;b. Float&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#float&quot;&gt;Floats&lt;/a&gt; are decimal numbers with or without fractional parts.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; temperature_degree_centigrade = -5.5
&amp;gt;&amp;gt;&amp;gt; isinstance(temperature_degree_centigrade, float)
True
&amp;gt;&amp;gt;&amp;gt; price = 99.99
&amp;gt;&amp;gt;&amp;gt; isinstance(price, float)
True
&amp;gt;&amp;gt;&amp;gt; payment = 500.0
&amp;gt;&amp;gt;&amp;gt; isinstance(payment, float)
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;c. Complex&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#complex&quot;&gt;Complex&lt;/a&gt; numbers are numbers that consist of a real and an imaginary part, it is represented as x + yj, where x is the real part, y is the imaginary part, and j is the imaginary unit or the square root of -1.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; m = 1 + 3j
&amp;gt;&amp;gt;&amp;gt; isinstance(m, complex)
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;d. Fraction&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/fractions.html#fractions.Fraction&quot;&gt;Fractions&lt;/a&gt; are numbers represented by a whole number numerator and another whole number denominator.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from fractions import Fraction
&amp;gt;&amp;gt;&amp;gt; a = Fraction(223/257)
&amp;gt;&amp;gt;&amp;gt; isinstance(a, Fraction)
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;e. Decimal&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/decimal.html#decimal.Decimal&quot;&gt;Decimal types&lt;/a&gt; are more precise than the float type. This is useful in financial calculations.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from decimal import Decimal
&amp;gt;&amp;gt;&amp;gt; interest_rate = Decimal(0.157)
&amp;gt;&amp;gt;&amp;gt; interest_rate
Decimal('0.1570000000000000006661338147750939242541790008544921875')
&amp;gt;&amp;gt;&amp;gt; isinstance(interest_rate, Decimal)
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;f. Boolean&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#bool&quot;&gt;Boolean&lt;/a&gt; takes two values either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; is_running = True
&amp;gt;&amp;gt;&amp;gt; is_sky_diving = False
&amp;gt;&amp;gt;&amp;gt; isinstance(is_running, bool)
True
&amp;gt;&amp;gt;&amp;gt; is_running
True
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;isinstance is used to check the data type of an object.&lt;/span&gt;
&lt;/div&gt;
&lt;h4&gt;g. Set&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#set&quot;&gt;Set&lt;/a&gt; is a collection of unique elements. They are useful for duplicate removals and membership checking.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; myset = {45, 60, 5, 3, 1, 15, 5, 45}
&amp;gt;&amp;gt;&amp;gt; # 45 and 5 are duplicates, set automatically removes them
&amp;gt;&amp;gt;&amp;gt; myset
{1, 3, 5, 60, 45, 15}
&amp;gt;&amp;gt;&amp;gt; # check the presence of 3 in the set
&amp;gt;&amp;gt;&amp;gt; 3 in myset
True
&amp;gt;&amp;gt;&amp;gt; # add 10 in the set
&amp;gt;&amp;gt;&amp;gt; myset.add(10)
&amp;gt;&amp;gt;&amp;gt; myset
{1, 3, 5, 10, 60, 45, 15}
&amp;gt;&amp;gt;&amp;gt; # remove the first value in the set which is 1
&amp;gt;&amp;gt;&amp;gt; myset.pop()
1
&amp;gt;&amp;gt;&amp;gt; myset
{3, 5, 10, 60, 45, 15}
&amp;gt;&amp;gt;&amp;gt; # remove all values in the set
&amp;gt;&amp;gt;&amp;gt; myset.clear()
&amp;gt;&amp;gt;&amp;gt; myset
set()
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;h. Dictionary&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#dict&quot;&gt;Dictionary&lt;/a&gt; is a data type that belongs to mapping key and value pairs. It is like a lookup table where you supply the key and you will get the value mapped on that key.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; user = {'name': 'john', 'age': 32, 'userame': 'jj'}
&amp;gt;&amp;gt;&amp;gt; # get the name
&amp;gt;&amp;gt;&amp;gt; user['name']
'john'
&amp;gt;&amp;gt;&amp;gt; # get the age
&amp;gt;&amp;gt;&amp;gt; user['age']
32
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;i. List&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#list&quot;&gt;List&lt;/a&gt; is part of the sequence type. It stores objects in sequence.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; # list of str or string
&amp;gt;&amp;gt;&amp;gt; fruits = ['mango', 'apple', 'avocado']
&amp;gt;&amp;gt;&amp;gt; fruits
['mango', 'apple', 'avocado']
&amp;gt;&amp;gt;&amp;gt; # get the first fruit in the list
&amp;gt;&amp;gt;&amp;gt; fruits[0]
'mango'
&amp;gt;&amp;gt;&amp;gt; # get the index of mango
&amp;gt;&amp;gt;&amp;gt; fruits.index('mango')
0
&amp;gt;&amp;gt;&amp;gt; # list of numbers
&amp;gt;&amp;gt;&amp;gt; numbers = [25, 45, 36]
&amp;gt;&amp;gt;&amp;gt; # list of dictionary
&amp;gt;&amp;gt;&amp;gt; users = [{'name': 'gina', 'username': 'gin'}, {'name': 'peter', 'username': 'pet'}]
&amp;gt;&amp;gt;&amp;gt; # list is mutable meaning you can change its value without changing its memory address
&amp;gt;&amp;gt;&amp;gt; phone_numbers = ['09290651058', '09284056958']
&amp;gt;&amp;gt;&amp;gt; # get the memory address
&amp;gt;&amp;gt;&amp;gt; old_id = id(phone_numbers)
&amp;gt;&amp;gt;&amp;gt; old_id
2034039969664
&amp;gt;&amp;gt;&amp;gt; # add a new phone number
&amp;gt;&amp;gt;&amp;gt; phone_numbers.append('09280232869')
&amp;gt;&amp;gt;&amp;gt; phone_numbers
['09290651058', '09284056958', '09280232869']
&amp;gt;&amp;gt;&amp;gt; new_id = id(phone_numbers)
&amp;gt;&amp;gt;&amp;gt; # if old_id and new_id are the same that means the address is not changed
&amp;gt;&amp;gt;&amp;gt; old_id == new_id
True
&amp;gt;&amp;gt;&amp;gt; # since the id's are the same, list is mutable
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;j. Tuple&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#tuple&quot;&gt;Tuple&lt;/a&gt; is similar to a list except it is immutable. It is defined by enclosing the values in a parenthesis.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; mytuple = (25, 9, 78)
&amp;gt;&amp;gt;&amp;gt; # get the value at index 0
&amp;gt;&amp;gt;&amp;gt; mytuple[0]
25
&amp;gt;&amp;gt;&amp;gt; # to define a single-valued tuple, add a comma
&amp;gt;&amp;gt;&amp;gt; tuple_single = (12,)
&amp;gt;&amp;gt;&amp;gt; # get the type of tuple_single
&amp;gt;&amp;gt;&amp;gt; type(tuple_single)

&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;k. Range&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#range&quot;&gt;Range&lt;/a&gt; is used to store sequences of numbers.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; myrange = range(0, 5, 1)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;0 is the start argument, 5 is stop argument, and 1 is the step argument.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; type(myrange)

&amp;gt;&amp;gt;&amp;gt; # get the value at index 1
&amp;gt;&amp;gt;&amp;gt; myrange[1]
1
&amp;gt;&amp;gt;&amp;gt; # print the sequence
&amp;gt;&amp;gt;&amp;gt; for n in myrange:
...     print(n)
...
0
1
2
3
4
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;l. Str&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#str&quot;&gt;String or str&lt;/a&gt; data type stores sequences of characters. String objects are immutable. &lt;/p&gt;
&lt;p&gt;Double quotes&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;region = &quot;South East Asia&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Single quote&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;car_brand = 'Tesla'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Triple double quotes&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;country_name = &quot;&quot;&quot;Philippines&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Triple single quote&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;france_capital = '''Paris'''
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since &lt;code&gt;str&lt;/code&gt; is a sequence type we can get its index values.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; myname = 'Ferdinand'
&amp;gt;&amp;gt;&amp;gt; myname[0]
'F'
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;m. Bytes&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#bytes&quot;&gt;Bytes&lt;/a&gt; data type is a sequence of byte, they are immutable.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; bytes_name = b'peter'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Convert to binary.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; binary = []
&amp;gt;&amp;gt;&amp;gt; for byte in bytes_name:
...     binary.append(format(byte, '08b'))
...
&amp;gt;&amp;gt;&amp;gt; binary
['01110000', '01100101', '01110100', '01100101', '01110010']
&amp;gt;&amp;gt;&amp;gt; # p = '01110000', e = '01100101' and so on.
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;n. Bytearray&lt;/h4&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#bytearray&quot;&gt;Bytearray&lt;/a&gt; is a byte sequence type similar to bytes. But bytearray is mutable and sequence entry can be modified.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;message = b'How are you?'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can change that message to bytearray.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;message_ba = bytearray(message)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let's change the &quot;H&quot; to &quot;N&quot;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; message = b'How are you?'
&amp;gt;&amp;gt;&amp;gt; message_ba = bytearray(message)
&amp;gt;&amp;gt;&amp;gt; # convert ascii N to a number as ord(&quot;N&quot;)
&amp;gt;&amp;gt;&amp;gt; message_ba[0] = ord(&quot;N&quot;)
&amp;gt;&amp;gt;&amp;gt; message_ba
bytearray(b'Now are you?')
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-faq&quot;&gt;
&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt;
&lt;span class=&quot;faq-body&quot;&gt;
1. How to know the data type of a variable or object?&lt;br&gt;
Answer: Use the type(object) class constructor.&lt;br&gt;&lt;br&gt;

2. How to get the memory address of an object?&lt;br&gt;
Answer: Use the id(object) function.&lt;br&gt;&lt;br&gt;

3. What is mutable and immutable objects?&lt;br&gt;
Answer: Mutable objects are those that we can change its value without changing the memory address such as &quot;list&quot;, &quot;dict&quot; and others. Immutable objects are the opposite of mutable such as &quot;str&quot;, &quot;tuple&quot; and others.
&lt;/span&gt;
&lt;/div&gt;
&lt;h2&gt;D. Summary&lt;/h2&gt;
&lt;p&gt;Our world is full of &lt;strong&gt;variables&lt;/strong&gt; and &lt;strong&gt;data types&lt;/strong&gt;. When you are travelling riding a bus at a speed of 40 KPH how long would it take if your destination is 60 KM away? We have an integer of 40 and 60 and assign a variable elapse to calculate the time in hours.&lt;/p&gt;
&lt;p&gt;The art of variable naming is essential to easily understand the written code. If your variable is about a number of some oceans, use &lt;code&gt;num_oceans&lt;/code&gt; instead of &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The most common data types that you will encounter are the &lt;strong&gt;integer, float, string, and list&lt;/strong&gt;. Dictionary is one of the most useful types that you can use as you progress. Ultimately you will create your &lt;strong&gt;data type&lt;/strong&gt; through class definitions.&lt;/p&gt;
</description>
<category>Python Basics</category>
<guid isPermaLink="true">https://coderlegion.com/201/python-variables-and-data-types</guid>
<pubDate>Sat, 23 Mar 2024 12:00:17 +0000</pubDate>
</item>
</channel>
</rss>