Posted December 15, 2022 by up2146107
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Text Type: | str
|
Numeric Types: | int , float , complex
|
Sequence Types: | list , tuple , range
|
Mapping Type: | dict
|
Set Types: | set , frozenset
|
Boolean Type: | bool
|
Binary Types: | bytes , bytearray , memoryview
|
None Type: | NoneType
|
You can get the data type of any object by using the type() function:
x = 5
print(type(x))
this principle goes for the same as
x = 24
print(type(x))
in python there is a lot of math based methods in return allow you to return the factorial of a number but bare that in mind that this method will only work if it has positive integers. The factorial of a number is the sum of the multiplications of the whole number from the number that we specify down to 1. for example the factorial of 6 would be 6x5x4x3x2x1 = 720.
Python has no command for declaring a variable, A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type, and can even change type after they have been set.
Example :
x = 20 # x is of type int
x = "Oakley" # x is now of type str
print(x)
If you want to specify the data type of a variable, this can be done with casting.
Example:
x = str(7) # x will be '7'
y = int(7) # y will be 7
z = float(7) # z will be 7.0
A variable can have a short name (like x and y) or a more descriptive name (age, car name, total volume). Rules for Python variables:
A variable name must start with a letter or the underscore character
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)
Example :
myvar = "Oakley"
my_var = "Oakley"
_my_var = "Oakley"
myVar = "Oakley"
MYVAR = "Oakley"
myvar2 = "Oakley"
Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable:
Camel Case;
Each word, except the first, starts with a capital letter:
myVariableName = "Oakley"
Pascal Case;
Each word starts with a capital letter:
MyVariableName = "Oakley"
Snake Case ;
Each word is separated by an underscore character:
my_variable_name = "Oakley"
Python allows you to assign values to multiple variables in one line:
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)