Python Data Types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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:
Getting the Data Type You can get the data type of any object by using the
Setting the Data Type In Python, the data type is set when you assign a value to a variable:
Setting the Specific Data Type If you want to specify the data type, you can use the following constructor functions:
Python Numbers There are three numeric types in Python:
Variables of numeric types are created when you assign a value to them:
To verify the type of any object in Python, use the
Int Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Float Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
Float can also be scientific numbers with an "e" to indicate the power of 10.
Complex Complex numbers are written with a "j" as the imaginary part:
Type Conversion You can convert from one type to another with the
Random Number Python does not have a
Python Casting: Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions:
Python Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the
Assign String to a Variable Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
Multiline Strings You can assign a multiline string to a variable by using three quotes:
Strings are Arrays Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
Looping Through a String Since strings are arrays, we can loop through the characters in a string, with a
String Length To get the length of a string, use the
Check String To check if a certain phrase or character is present in a string, we can use the keyword
Check if NOT To check if a certain phrase or character is NOT present in a string, we can use the keyword
Tags: Python Python Casting Python Data Types Python Numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Login for comment |
OTHER POSTS IN THE SAME CATEGORYPython Classes/ObjectsPython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython DictionariesPython SetsPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python ListsPython String OperationsPython VariablesPython CommentsPython SyntaxPython Getting StartedPython IntroductionWhat is Python? |