By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Variables in any programming language are the names of the reference to the reserved memory locations which are used to store values. Similarly, when we are creating a variable in Python then we are reserving some space in the memory. These variables have their own data type. Based on the type, the interpreter allocates memory and decides what kind of data can be stored in these reserved memory locations.
Characters, integers, decimals, etc. are the different data types which can be assigned to such variables. Assigning Values to Variables In the Python language, equal sign (=) is used to assign values to the variables. Such variables do not need explicit declaration. When we assign a value to a variable, the declaration or creation happens automatically. The operand to the left of the equal sign (=) is the name of the variable and the operand to the right of the equal sign (=) is the value stored in the variable.
This is demonstrated in the below example. In the above example, the variable name ‘number’ has an integer value therefore, it behaves as an integer without any data type declaration. Similarly, the variable name ‘decimal’ has a floating value and variable name ‘name’ has a string value. Python is a THvery flexible language since it automatically determines the data type once the value is assigned to the variable. Multiple Assignment The Python language allows the assignment of a single value to more than one variables and multiple values to multiple variables which are separated by commas in a single line as demonstrated in the below example. In the first case (many-t0-one), the single value 1000 is assigned to many variables a, b, c and d. In the second case (many-to-many), multiple values ("Jose", "Patrick", "Peter") are assigned to multiple variables k, l and m. However, here is one to one mapping between a variable and a value, e.g. variable k will contain value as “Jose”, variable l will contain value as “Patrick” and variable m will contain value as “Peter”. Standard Data Types in Python In Python, the data is stored in memory which can be of many types. For example, a person's birth year is stored as a numeric value and his or her qualifications are stored as alphanumeric characters. Depending on the type of value, the Python has different standard data types that are used to define the type of value a variable can contain. Python language has five standard data types. We are going to discuss them in detail. These are: - Numbers - Strings - Lists - Tuples - Dictionary Python Numbers In Python language, the number data type are used to store numeric values. Numeric variable are created automatically in Python when we assign a numeric value to it as shown in the below example. Python supports below four different numerical types. - int (signed integers) - long (long integers, they can also be represented in hexadecimal and octal) - float (floating point real values) - complex (complex numbers) Below are the examples of number objects in Python language.
Int
Long
float
Complex
40
7965391L
0.0
8.14j
900
-0x29546L
17.90
675.j
-589
0455L
-31.9
23.8922e-36j
050
0xABDDAECCBEABCBFEACl
62.3+e68
.6776j
-0630
563213626792L
-560.
-.6844+0J
-0x1290
-032318432823L
-82.53e200
4e+86J
0x37
-5627995245529L
40.2-E52
7.59e-7j
Below are few things to note about Python number objects. - A complex number consists of an ordered pair of real floating-point numbers denoted by (real + imgj), where real and img are the real numbers and j is the imaginary number unit. - Python displays long integers (data type number) with an uppercase L. - Python language allows to use a lowercase L with long data type number, however it is recommended to use only an uppercase L in order to avoid confusion with the number 1. In Python, we can delete the reference to a number object (variable) by using the ‘del’ statement.
Given below is the syntax of the ‘del’ statement.
del variable1[,variable2[,variable3[....,variableN]]]]
Using the above statement, we can delete a single variable or multiple variables by using the ‘del’ statement as shown in the below example. In the above example, since we have deleted variable2 using the ‘del’ command, this variable do not exist anymore when we tried to print it. Strings in Python In Python language, Strings are identified as a contiguous set of characters which are represented within the quotation marks. Python language permits the use of single ('), double (") and triple (''' or """) codes to represent a string literal, making sure that the same type of quote begins and ends that string.
Strings in Python have below operators. - Slice operator ([ ] and [:]). By using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end, subsets of strings can be taken. - Plus (+) sign operator. By using the plus (+) sign operator, we can concatenate two or more strings. - Asterisk (*) sign operator. Asterisk operator is the repetition operator. If we want to print string 3 times, then simply we can give command as print (string * 3).
All of above operators are demonstrated in the below example. Lists in Python In Python language, a List is the most versatile compound data types. A list contains items which are separated by commas and enclosed within square brackets ([]). Lists are similar to arrays in C or C++ in some extents. The difference between arrays in C /C++ and lists in Python is that the former cannot have different datatype for elements while latter can have different datatype for elements. Lists in Python have below operators. - Slice operator ([ ] and [:]). By using the slice operator ([ ] and [:]) with element position starting at 0 in the beginning of the list and working their way from -1 at the end, subsets of the list can be taken. - Plus (+) sign operator. By using the plus (+) sign operator, we can concatenate two or more lists. - Asterisk (*) sign operator. Asterisk operator is the repetition operator. If we want to print a list 2 times, then simply we can give command as print (listsdemo * 2). Tuples in Python In Python language, a tuple is a sequence data type which is almost similar to the list. A tuple consists of a number of values which are comma separated. Unlike lists, tuples are enclosed within parentheses. The main differences between tuples and lists are as follows. - Tuples are enclosed in parentheses (( )) whereas Lists are enclosed in brackets ([ ]). - Tuples are read-only lists as their elements and size cannot be changed, while Lists can be updated. We can change lists elements and size. Tuples in Python have below operators. - Slice operator ([ ] and [:]). By using the slice operator ([ ] and [:]) with element position starting at 0 in the beginning of the tuple and working their way from -1 at the end, subsets of the tuple can be taken. - Plus (+) sign operator. By using the plus (+) sign operator, we can concatenate two or more tuples. - Asterisk (*) sign operator. Asterisk operator is the repetition operator. If we want to print a tuple 2 times, then simply we can give command as print (tuplesdemo * 2). Dictionary in Python A dictionary in Python represents hash table.
A hash table (or hash map) is a data structure which is used to implement an associative array, a structure that can map keys to values. To compute an index of an array of buckets or slots, a hash table uses a hash function to procure the desired value. This concept in Python work like associative arrays or hashes found in Perl and consist of key-value pairs. Keys in Python dictionary can be of any data type, however mostly they are either numbers or strings. On the other hand, values in Python dictionary are Python objects.
In Python, syntax wise there are two ways dictionaries can be created which are mentioned below:
Dictionary name is given with curly braces ({ }) first (E.g. veggie = {}). Next we can define the key value pairs one by one as (E.g. veggie ["tomatoes"] = 20). Here, key is tomatoes and the value is 20.
Dictionary can also be defined with all key value pairs in one go within the curly braces ({}). (E.g. fruits = {'apple': 'Good','banana':'Better', 'orange': 'Best'}). Here, dictionary name is ‘fruits’, ‘apple’ is one of the key of such dictionary and ‘Good’ is the associated value with this key.
These syntaxes are demonstrated in the below example. Data Type Conversion While writing programming code, we may need to perform data type conversions. To support such operations, Python language has several built-in functions which are used to perform conversion from one data type to another. After conversion, these functions return a new object representing the converted value. Below is the list of Python built-in functions along with their operational description.
Function
Description
int(value [,Base])
This function converts value into an integer. “Base” specifies the base if value is a string.
long(value [,Base] )
This function converts value into a long integer. “Base” specifies the base if value is a string.
chr(value)
This function converts an integer into a character.
complex(real [,imag])
This function is used to create a complex number.
dict(Value)
This function is used to create a dictionary. “Value” must be a sequence of (key, value) tuples.
eval(strg)
This function is used to evaluate a string which returns an object.
float(value)
This function converts value into a floating-point number.
frozenset(value)
This function converts value into a frozen set.
hex(value)
This function converts an integer value into a hexadecimal string.
list(value)
This function converts value to a list.
repr(value)
This function is used to convert an object value to an expression string.
oct(value)
This function is used to converts an integer value to an octal string.
ord(value)
This function is used to converts a single character to its integer value.
set(value)
This function is used to convert value into a set.
str(value)
This function is used to convert an object value into a string representation.
tuple(value)
This function is used to convert value into a tuple.
unichr(value)
This function is used to convert an integer value into a Unicode character.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.