3. Python Variables and Data Types


3.1 Variables

Variables are reserved memory locations to store values. Python interpreter will allocates and decides what can be stored in memory. 

3.1.1 Identifier

Python identifier is a rule to define the name of variables, functions, class, module, and objects. 

These are naming conventions in Python.
  • An identifier start with A-Z or a-z or an underscore (_) followed by number (0-9).
  • Punctuation is not allowed, such as @, $, % when defining identifier.
  • Case sensitive, so name and Name is different.
  • Class name start with an uppercase.
  • Identifier leads by single underscore indicates it's private identifier.
  • Identifier leads by two underscores indicates it's strongly private identifier.
  • Identifier ends with two underscores indicates it's language-defined special name.
Some reserved words in Python that is not allowd to be used as identifier.

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

3.1.2 Variable Assignment

Some ways to do variable assignment in Python are.
print("Single assigment: ")
name = "Lee"    
age = 20
marks = 80.4
print(name)
print(age)
print(marks)
print("Multiple objects assignment: ")
name, age, marks = "Lucy", 20, 90.2
print(name)
print(age)
print(marks)
print("Single Assignment multiple variables: ")
a = b = c = 7
print(a)
print(b)
print(c) 
The program produce 



3.2 Data Types

Everything in Python is an object, so data types are actually classes and variables are object of these classes. You can use type() function to identify which class a variable and value type. Python 3 support the following data types. 

3.2.1 Numbers

Integers, floating points, and complex numbers are part of Python numbers. They are defined as int, float, and complex class. Example
a = 34
print(a, "is belong to", type(a))

b = 23.1
print(b, "is belong to", type(b))

c = 1 + 3j
print(c, "is belong to", type(c))


3.2.2 String

String is defined as sequence of characters which enclosed either with single quote or double quote. Example
first_name = "Nail"
last_name = "Smith"
print("first_name is belong to", type(first_name))
print("last_name is belong to", type(last_name))

3.2.3 List

List is defined as collections of objects which enclosed within square brackets. Example
port_num = [80, 443, 21, 22, 52]
list_example = ["string", 23.4, 234, 1 + 3j]
print("port_num is belong to", type(port_num))
print(port_num) 
print(list_example)


3.2.4 Tuple

Similar with list, it's just enclosed within parentheses. Example
port_num = (80, 443, 21, 22, 52)
list_example = ("string", 23.4, 234, 1 + 3j)
print("port_num is belong to", type(port_num))
print(port_num) 
print(list_example)


3.2.5 Set

Set is defined as collections of unordered unique item and enclosed within curly braces. Example
set_number = {2, 2, 2, 3, 3, 1, 1, 1, 1, 9}
set_name = {'John', 'Andy', 'Smith', 'Andy', 'Ben'}
print("set_number is belong to", type(set_number))
print(set_number)
print(set_name)    


3.2.6 Dictionary

Dictionary is defined as collections of key:value pairs and enclosed within curly braces. Example
example = {}        # define empty dictionary
example['one'] = 1  # assign new value key 'one', value 1
example['two'] = 2  # assign new value key 'two', value 2
example2 = {
        'one': 1,
        'two': 2
    }
print("example is belong to", type(example))
print(example)
print(example2) 

Subscribe to receive free email updates:

0 Response to "3. Python Variables and Data Types"

Post a Comment