This content originally appeared on DEV Community and was authored by oladejo abdullahi
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
codes>>
x = 6
name = "Maxwizard"
age="25years"
phone=07045225718
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 whenver 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)
Result>>
Maxwizard
07045225718
15
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:
- Text Type: str
- Numeric Types: int, float, complex
- Set Types: set, frozenset
- Boolean Type: bool
- Binary Types: bytes, bytearray, memoryview
- Sequence Types: list, tuple, range
- 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","*$%#@^!"
? 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 i represent the square root of -1
– Example: 4 + 2i , 5 - 5i
? 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.
Example
? 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)
Result>>
"jss1","jss2","jss3","ss1","ss2","ss3"
oxford',"uni-Ibadan"
? 8. Set: these are used to store multiple items in a single
A set is a collection which is both unordered and unindexed.
Sets are written with curly brackets "{}".
Example:
class_in_secondary={"jss1","jss2","jss3","ss1","ss2","ss3"}
school={'oxford',"uni-Ibad"}
print(school)
Example
? 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"])
Result>>
{'name': 'Oladejo Abdullahi', 'sex': 'male', 'status': 'single', 'school': 'university of Ibadan', 'Age': 25}
University of Ibadan
single
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
This content originally appeared on DEV Community and was authored by oladejo abdullahi
oladejo abdullahi | Sciencx (2021-09-02T17:54:07+00:00) Data type in python. Retrieved from https://www.scien.cx/2021/09/02/data-type-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.