Types Summary and Type Conversion

Types summary

The following is a summary of the different Python types:

  • string - Sequence type: Used for text.
  • list- Sequence type: A mutable container with ordered elements.
  • tuple - Sequence type: An immutable container with ordered elements.
  • set  - Set type: A mutable container with unordered and unique elements.
  • dict – Mapping type: A container with key-values associated elements.

Type conversion

Type conversion means converting from one data type to another. For example, if you read a number from input, it is read as a string, but you may want to convert it to an integer or floating-point in order to use it in a calculation later.

An implicit conversion is a type conversion that the interpreter does automatically. This typically happens between numeric types, for example if you were to add integer 5 + integer 3, it would return an integer 8, but if you add integer 5 + floating-point value 3.0, it would return a floating point value 8.0

There are some useful conversion functions that you can use:

  • int(value) – converts value to an integer (You can pass a string that looks like it can be an integer, i.e. ‘25’)
  • float(value) – converts value to a floating-point (You can pass a string that looks like it can be a floating-poing, i.e ’3.14’)
  • str(value) – converts value to a string
Search
Categories
Read More
Computer Programming
Using the <span> Tag for Styling
The <span> tag is a generic HTML element that is often used to group inline elements...
By HTML PROGRAMMING LANGUAGE 2024-08-29 02:17:00 0 2K
Physics
ASSHU ANKOLE MOCK S.6 PHYSICS PAPER 1 GUIDE 2024
ASSHU ANKOLE MOCK S.6 PHYSICS PAPER 1 GUIDE 2024
By Landus Mumbere Expedito 2024-08-14 07:01:43 0 3K
Physics
S.6 Physics 2 Facilitation 2024
https://acrobat.adobe.com/id/urn:aaid:sc:EU:fd4a0074-f4ff-48b8-9f7c-955b2dd2b3f2
By Landus Mumbere Expedito 2024-07-19 13:10:33 0 3K
Technology
Ethical Challenges in Information Technology
The rapid advancement and widespread adoption of information technology (IT) have brought about...
By ALAGAI AUGUSTEN 2024-07-15 18:02:31 0 2K