DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

Data type in python

Variables

Variables are containers for storing data values.
Python has no command for declaring a variable. the moment you assign a value to any word or letter, variable is created.

Example1

x = 6
name = "Maxwizard"
age="25years"
phone="07045225718"
Enter fullscreen mode Exit fullscreen mode

So in example1 above x,name,age and phone are variable name. and can be called anytime to refers to the value they stored.
i.e whenever you called 'x' you are actually mean 6. while "phone" always means "07045225718".
Variables do not need to be declared with any particular type,
and can even change type after they have been set. let assume
we need to call them we will use print() function. Note that
print() is one of the function to display your output when you write codes.

Example2

write the following code and run it.
codes>>

x = 6
y=9
name = "Maxwizard"
age="25years"
phone="07045225718"
print(name)
print(phone)
print(x+y)
Enter fullscreen mode Exit fullscreen mode

Result>>

Maxwizard
07045225718
15
Enter fullscreen mode Exit fullscreen mode

can you see the result, we just ask the interpreter to print name,phone and x+y. and it printed what they stored.

**How to Declare Variable Name
πŸ”₯ Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for R variables:
πŸ”₯ A variable name must start with a letter
πŸ”₯ A variable name cannot start with a number
πŸ”₯ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
πŸ”₯ Variable names are case-sensitive (age, Age and AGE are three different variables)

Variables

variable can be categorize into different categories base on the type of data they stored. they are:

Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
the following data types are built-in by default, in these categories:

  1. Text Type: str
  2. Numeric Types: int, float, complex
  3. Set Types: set, frozenset
  4. Boolean Type: bool
  5. Binary Types: bytes, bytearray, memoryview
  6. Sequence Types: list, tuple, range
  7. Mapping Type: dict

πŸ’₯ 1. Character/String: they are non-number that is they are always alphabetic word or alpha-numeric or symbols.
Examples: "a", "everyone",'of' "u2CanCode","*$%#@^!"

let us see example using codes
Codes

noun="Tolani "
verb='is '
preposition="in "
place='abuja'
print(verb)
print(noun + verb + preposition+place)
print("I can print a sentence without assigning it to a value directly as string")
Enter fullscreen mode Exit fullscreen mode

Result>>

is 
Tolani is in abuja
I can print a sentence without assigning it to a value directly as string
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ 2. floats
– These are the decimal numbers default type when dealing with numbers.
Examples: 3.531, 1.0, 42.5, 4/9

πŸ’₯ 3. Integers: these are the counting number which must be whole number.
– Examples: 1, 2, 42

πŸ’₯ 4. Complex: they are the imaginary number where their j represent the square root of -1
– Example: 4 + 2j , 5 - 5j
Let see some codes solving complex number problem.
simplify the following.

  1. 4+5i/4-5i
  2. 4i^5i
  3. (4+3i)^2/6i
  4. (5+8i)^(3-i) Codes>>
x=(4+5j)/(4-5j)
print(x)
y=4i**5i
print(y)
z=(4+3i)**2/6i
print(z)
k=(5+8i)**(3-1i)
print(k)
Enter fullscreen mode Exit fullscreen mode

Result>>

(-0.21951219512195122+0.9756097560975611j)
(0.0003094443893170943+0.000234405412606401j)
(4-1.1666666666666667j)
(1622.3902605503722+1644.8518784941748j)
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ 5. Boolean:
– Two possible values: TRUE and FALSE
– You can also use T and F, but this is not recommended.
– NA is also considered logical.

πŸ’₯ 6. List: these are used to store multiple items in a single variable.they are created using square brackets "[]":

Examples:

name=["maxwizard","sesan",'Timothy','Wareez']
age=[12,14,18,20,23]
πŸ’₯ 7. Tuple: these are used to store multiple items in a single variable. they are created using round bracket. and they are usullay ordered and unchangeable.

Example

Code>>

class=("jss1","jss2","jss3","ss1","ss2","ss3"}
school=('oxford',"uni-Ibadan")
print(class)
print(school)
Enter fullscreen mode Exit fullscreen mode

Result>>

"jss1","jss2","jss3","ss1","ss2","ss3"
oxford',"uni-Ibadan"
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ 8. Set: these are used to store multiple items in a single
A set is a collection which is both unordered and un-indexed.
Sets are written with curly brackets "{}".

Example:
class_in_secondary={"jss1","jss2","jss3","ss1","ss2","ss3"}
school={'oxford',"uni-Ibad"}
print(school)
Enter fullscreen mode Exit fullscreen mode

Result>>

"jss1","jss2","jss3","ss1","ss2","ss3"
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ 9. Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is unordered, changeable and does not allow duplicates.
Dictionaries are written with curly brackets, and have keys and values:

Example

** Codes>>**

biodata={"name": "Oladejo Abdullahi",
        "sex":"male",
        "status": "single",
        "school": "university of Ibadan",
        "Age":25,
        }
print(biodata)
print(biodata["school"])
print(biodata["status"])
Enter fullscreen mode Exit fullscreen mode

Result>>

{'name': 'Oladejo Abdullahi', 'sex': 'male', 'status': 'single', 'school': 'university of Ibadan', 'Age': 25}
University of Ibadan
single

Enter fullscreen mode Exit fullscreen mode

study the codes and the results above. the keys are : sex,status,school and age. while their values are what you see after column.
so the wholecodes is like saying
"name": "Oladejo Abdullahi" means name=Oladejo
"sex":"male" means sex=male
"status": "single" means status = single
"school": "university of Ibadan" means school = university
"Age":25 means age=25
the only different is that you have to join it with biodata.
Above are the most important data type that we used in python.
we shall do fully discussion on each one of them in the next lesson. I hope you find this helpful?? consider to share and like you can also comment below if you have addition or question. chat me up on 09153036869. or on Facebook you can also follow me on Instagram for more update

Discussion (0)