flyingloha.blogg.se

Basic data types and operations
Basic data types and operations











basic data types and operations

Dictionary (dict)Ī dictionary is a set of key-value pairs. TypeError: 'tuple' object does not support item assignment 5. # Unlike lists, the elements of a tuple cannot be changed. > squares # Fetching all elements of a tuple # Slicing the tuple to get a selection of items # Accessing the items of a tuple based on the index of the item # Finding the length of the tuple using the in-built len() function The following examples showcase results of different types of tuple operations. # variable2 is a tuple storing different data types Tuples are like lists, except for the fact that the elements of a tuple cannot be changed after initialization. The elements of a tuple are surrounded by round brackets, that is, ( ). Tuples are array sequences that store a collection of items of the same or different data types.

basic data types and operations

# Clear the list by replacing all the elements with an empty list > squares = # Removing items at index 1 to 3 > squares = 12 # Assigning item at index 3 as 12 > squares # Fetching all elements of the list > squares # Fetching all elements from index 1 to index 3 > squares # Fetching all elements except the last two elements > squares # Fetching all elements till and not including index 2 > squares # Fetching all elements till and not including index 1 > squares # Fetching all elements starting from index 2 > squares # Fetching all elements starting from index 1 # Slicing the list to get a selection of items > squares # -2 index fetches the second last item > squares # -1 index fetches the last item > squares # 0 index fetches the first item # Accessing the items of a list based on the index of the item # Finding the length of the list using the in-built len() function The following examples showcase results of different types of list operations. # variable2 is a list storing different data types Lists are one of the most used data types in Python other than strings or numbers. This means that the first item of a list has an index of 0, the second item of a list has an index of 1, and so on. The elements of a list are surrounded by square brackets and the item indexing starts at 0.

basic data types and operations

Lists are array sequences that store a collection of items of the same or different data types.

basic data types and operations

# Use of different arithmetic operations may return an integer or a floating-point number # The ** operator returns x to the power of y. # The % operator returns the remainder of the division # Floor division discards the fractional part and return an integer # A floating-point number is accurate up to 15 decimal places # Division always returns a floating-point number # Addition/subtraction of an integer and a float returns a floating point number # Addition/subtraction of integers returns an integer The following examples showcase results of different arithmetic operations performed using integers and floats. They are also known as the numeric data types in Python. For example, the number 100 is an integer and the number 100.0 is a floating-point number. Floats (or floating-point numbers) are numbers with a decimal point. Integers are numbers without a decimal point. # Finding the length of the string using the in-built len() function # Multiplying a string by a number results in multiple concatenations of the same string # Adding two strings together results in string concatenation The examples demonstrated below showcase results from different types of string operations performed in Python. For example, # Both variable1 and variable2 are strings Variables of type String are surrounded by either single or double quotation marks. Now, let us move onto discuss the characteristics of some commonly used Python data types.īasic Data Types in Python 1. You can also use the in-built type() function in Python to get find the basic data type in Python. Here is a table containing all the information you need to understand about these data types in Python: Data Type In this tutorial, we will be discussing the 8 core basic data types in Python. Python contains several data types to make it easier for programmers to write functional and replicable programs. In this tutorial, you will learn about the 8 basic data types in Python. A data type specifies the type of value that a variable has and the type of operations that can be applied to the variable.













Basic data types and operations