2.3. Python Language Fundamentals: Data Types in Python

Página 5

2.3. Python Language Fundamentals: Data Types in Python

Python is a dynamically typed programming language, which means you don't need to explicitly declare the data type of a variable when you create it. The Python interpreter infers the data type based on the value you assign. However, understanding the different types of data available in Python is crucial to writing effective and efficient code. In this chapter, we will discuss the main data types in Python: numbers, sequences, sets, and mappings.

Numbers

Python supports three types of numbers: integers (int), floating point numbers (float) and complex numbers (complex). Integers can be of any length, while floating point numbers are real numbers that contain a decimal part. Complex numbers, on the other hand, are written in the form a + bj, where a and b are real numbers and j represents the square root of -1.

Sequences

Sequences in Python are an ordered collection of items. The three types of sequences in Python are strings, lists, and tuples. Strings are sequences of characters, while lists and tuples are sequences of any data type. The main difference between lists and tuples is that lists are mutable, which means you can change their elements, while tuples are immutable.

Sets

A set in Python is an unordered collection of unique items. Sets are useful when you want to maintain a collection of items, but don't care about their order, and want to ensure there are no duplicates. Python supports two types of sets: set and frozenset. The set type is mutable, which means you can add and remove items from it, while the frozenset is immutable.

Mappings

The only type of mapping in Python is the dictionary. A dictionary is an unordered collection of key-value pairs. Keys in a dictionary must be unique and immutable, meaning you can use strings, numbers, or tuples as keys, but not lists or other dictionaries. Values ​​in a dictionary can be of any data type.

Data Type Conversion

Python also supports conversion between different data types. You can use functions such as int(), float(), str(), list(), tuple(), set(), and dict() to convert data from one type to another. However, keep in mind that not all conversions are possible. For example, you cannot convert a string that contains non-numeric characters to a number.

Conclusion

In summary, Python supports a variety of data types that you can use to store and manipulate information in your code. Each type of data has its own characteristics and uses, so it is important to understand how and when to use them. In the next chapter, we will discuss the operations you can perform with these types of data.

Now answer the exercise about the content:

What are the three types of numbers supported by the Python programming language?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

62.4. Python Language Fundamentals: Operators in Python

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text