Data Type conversion in python part 10 | Number | Wire | The list | Tuple | Dictionary

Data type conversion in python | Number | Wire | The list | Tuple | Dictionary




Standard data type

A variable can hold different types of values. For example, a person's name should be stored as a string, while its ID should be stored as an integer.

Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.

NumberWireThe listTupleDictionary

In this part of the tutorial, we will give a brief introduction to the above data types. We will discuss each of them in detail later in this tutorial.

Number

A number stores numeric values. Python creates a number object when a number is assigned to a variable. for example;

a = 3, b = 5 # A and B are number objects
Python supports 4 types of numeric data.

int (signed integers such as 10, 2, 29, etc.)
Long (integer used for a higher range of values ​​such as 908090800L, -0x1929292L, etc.)
Float (Float is used to store floating point numbers such as 1.9, 9.902, 15.2, etc.)
Complex (complex numbers like 2.14j, 2.0 + 2.3j, etc.)
Python allows us to use a low-case L that can be used with long integers. However, to avoid confusion we should always use the upper position L.

A complex number consists of an ordered pair, i.e., x + iy where x and y represent real and imaginary parts, respectively).

Wire

A string can be defined as a sequence of characters represented in quotation marks. In Python, we can use single, double or triple quotes to define a string.

Handling Python is a straightforward task as it provides many inbuilt functions and operators.

In the case of string handling, operator + is used to connect two strings because the operation "hello" + "python" returns "halo python".

The operator * is referred to as the "operation" python "2 return" python python "as the iteration operator.

The following example shows string handling in Python.

str1 = 'hello javatpoint' #string str1
str2 = 'how are you' # string string2
Print (str1 [0: 2]) # opening the first two characters using the slice operator
Print (str1 [4]) # 4th character of string
Print (str1 * 2) # opening string twice
Print (str1 + str2) # denotes the composition of str1 and str2
Output:

She
O
Hello javatpointhello javatpoint
Hello javatpoint how are you

The list

C. Has lists similar to arrays. The list may contain different types of data. Items stored in the list are separated with commas (,) and enclosed within square brackets [].
We can use the slice [:] operators to access the data of the list. The concatenation operator (+) and the repetition operator (*) work with the list in the same way as they were working with strings.

Consider the following example.

l = [1, "hi", "dragon", 2]
Print (l [3:]);
Print (l [0: 2]);
Print (L);
Print (L + L);
Print (l * 3);
Output:

[2]
[1, 'hi']
[1, 'hi', 'dragon', 2]
[1, 'hi', 'dragon', 2, 1, 'hi', 'dragon', 2]
[1, 'Hi', 'Python', 2, 1, 'Hi', 'Python', 2, 1, 'Hi', 'Python', 2]

Tuple

A tuple is similar to a list in many ways. Like lists, tuples also contain a collection of objects of different data types. Tuple items are separated with commas (,) and enclosed in parentheses ().

A tuple is a read-only data structure because we cannot modify the size and value of objects in a tuple.

Let's look at a simple example of tuple.

T = ("hi", "dragon", 2)
Print (t [1:]);
Print (t [0: 1]);
Print (t);
Print (t + t);
Print (t * 3);
Print (type)
t [2] = "hi";
Output:

('Dragon', 2)
('Hello',)
('Hi', 'Python', 2)
('Hi', 'Python', 2, 'Hi', 'Python', 2)
('Hee', 'Python', 2, 'Hi', 'Python', 2, 'Hi', 'Python', 2)
<Type 'tuple'>
Traceback (most recent call last):
File "main.py", line 8, in <module>
t [2] = "hi";
TypeError: 'tuple' object does not support item assignment
Dictionary
The dictionary is a systematic set of one-valued-value pairs of objects. It is like an associative array or hash table where each key stores a specific value. The key can hold any primitive data type while the value is an arbitrary Python object.

Items in the dictionary are separated with commas and enclosed in curly braces {}.

Consider the following example.

d = {1: 'Jimmy', 2: 'Alex', 3: 'John', 4: 'Mike'};
Print ("first name" + d [1]);
Print ("another name" + d [4]);
Print (d);
Print (d.keys ());
Print (d.values ​​());
Output:

First name is jimmy
Another name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'John', 4: 'Mike'}
[1, 2, 3, 4]

['Jimmy', 'Alex', 'John', 'Mike']

Python data type

By Dale Fugier (Last modified: 05 December 2018)

This guide is an overview of Python data types.

Overview
There are five standard data types of Python:

Number

Wire

The list

Tuple

Dictionary

Python determines the variable type that is based on the value it is assigned to. Unlike more rigid languages, Python will change the variable type if the variable value is set to another value. for example:

var = 123 # This will create a number integer assignment
var = 'john' # `var` variable is now a string type.
Number
Python number variables are created by the standard Python method:

var = 382
Most of the time it is fine to use the standard Python number type. Python will automatically convert a number from one type to another, if it is needed. But, in some special circumstances that require a specific number type (eg. Complex, hexadecimal), the format can be forced into the format using additional syntax in the table below:

Write draft description

int a = 10 signed integers
Long = 345L (L) long integers, they can also be represented in octagonal and hexadecimal
Float A = 45.67 (.) Floating Point Actual Value
The complex a = 3.14J (J) contains integers in the range 0 to 255.

Most of the time Python will automatically perform variable conversions. You can also use the Python transformation function (int (), long (), float (), complex () to convert data from one type to another. Also, the type function allows your data to be stored within a variable. Gives information about how it is stored.

Message = "good morning"
Number = 85
pi = 3.14159

Print (type (message)) # this will return a string
Print (type (n)) # This will return an integer
Print (type (pi)) # this will return a float

Wire

Create string variables by inserting characters in quotes. Python uses double quotes of single quotes and triple quotes "" to denote literal strings. Only triple quoted strings "" "will also automatically continue at the end of the line statement.

First name = 'John'
Last Name = "Smith"
message = "" "This is a string that will span multiple lines. Using newline characters
And there is no space for the front lines. The end of the rows within this string are also counted as a new line when "" ""
Strings can be accessed as whole strings, or as an alternative to absolute variables using brackets be [] '. Here are some examples:

var1 = 'Hello World!'
var2 = 'RhinoPython'

Print var1 [0] # This will print the first letter `H` in the string
Print var2 [1: 5] # This will print the Substitute 'hinoP`
Python can use a special syntax to format multiple strings and numbers. The string formatter is covered quickly here as it is often seen and it is important to recognize the syntax.

"Item {} is repeated {} times" .format (element, count))
{} Are placeholders that are replaced by variable elements and counted in the last string. This compact syntax is meant to keep the code more readable and compact.

Python is currently transitioning to the above format syntax, but Python may use an older syntax, which is being phased out, but is still seen in some example code:

"Item% i repeated% i"% (element, count)
For more information on string formatter in Python, see the article: PyFormat Website

For a more detailed look at string variables in Python, see the TutorialSpot Python tutorial on sting. Note: This article uses the older formatting syntax, but contains much useful information.

The list

Lists are a very useful variable type in Python. A list can contain a range of values. List variables are declared using the variable [].

A = [] # This is a blank list variable
B = [1, 23, 45, 91] # This list makes a preliminary list of 4 numbers.
C = [2, 4, 'John'] # Lists can contain different types of variables.
All lists in Python are zero-based indexes. The number of list elements is always greater than the number indicated when referring to the length of a member or a list.

mylist = ['rhino', 'grasshopper', 'flamingo', 'bongo']
B = len (mylist) # This will return the length of the list which is 3.. The index is 0, 1, 2, 3.
Print mylist [1] # This will return the value at index 1, which is 'Grasshopper'
Print mylist [0: 2] # This will return the first 3 elements in the list.
You can assign data to a specific element of a list using an index in the list. The list index starts at zero. The data can be assigned to the elements of an array as follows:

mylist = [0, 1, 2, 3]
mylist [0] = 'Rhino'
mylist [1] = 'Grasshopper'
mylist [2] = 'flamingo'
mylist [3] = 'Bongo'
Print mylist [1]
The list is not limited to only one dimension. Although most people cannot understand more than three or four dimensions. You can declare multiple dimensions separated by commas. In the following example, the MyTable variable is a two-dimensional array:

MyTable = [[], []]
In a two-dimensional array, the first number is always the number of rows; The second number is the number of columns.

For a detailed look at manegi

Post a Comment

0 Comments

Search This Blog