Skip to main content

Python for Beginners - Python Data Types

Python supports several built-in data types, including:

Numeric Types:

Integer (int): Whole numbers without a fractional component.

Floating-point (float): Numbers with a fractional component.

Complex (complex): Numbers with a real and imaginary part.

Sequence Types:

String (str): A sequence of characters.

List (list): An ordered collection of items.

Tuple (tuple): An ordered, immutable collection of items.

Mapping Type:

Dictionary (dict): A collection of key-value pairs.

Set Types:

Set (set): An unordered collection of unique elements.

Frozen set (frozenset): An immutable version of a set.

Boolean Type:

Boolean (bool): Represents either True or False.

Binary Types:

Bytes (bytes): A sequence of bytes.

Bytearray (bytearray): A mutable sequence of bytes.

These are the main built-in data types in Python. Additionally, Python also allows you to define and work with custom data types using classes and objects.


Here are some examples of each data type:

Numeric Types:

# Integer
x = 10

# Floating-point
y = 3.14

# Complex
z = 2 + 3j

Sequence Types:

# String
name = "John Doe"

# List
numbers = [1, 2, 3, 4, 5]

# Tuple
coordinates = (10, 20)

Mapping Type:

# Dictionary
person = {"name": "John Doe", "age": 25, "city": "New York"}

Set Types:

# Set
fruits = {"apple", "banana", "orange"}

# Frozen set
vowels = frozenset(["a", "e", "i", "o", "u"])

Boolean Type:

# Boolean
is_valid = True
is_complete = False

Binary Types:

# Bytes
data = b'Hello World'

# Bytearray
buffer = bytearray([0x01, 0x02, 0x03, 0x04])

These examples demonstrate the usage of different data types in Python. You can perform various operations and manipulations on these data types based on their specific characteristics and methods.




Comments

Popular posts from this blog

Python for Beginners - Python Indentation

  Python Indentation In Python, indentation plays a crucial role in determining the scope and organization of code blocks. Python uses whitespace indentation to delimit blocks of code, rather than curly braces ({}) or similar constructs used in other programming languages. Here are some basic rules to keep in mind when it comes to indentation in Python: Use consistent indentation throughout your code. Indent your code with four spaces (not tabs). Indentation level should be consistent for all statements within a block of code. Indentation should be used to indicate the scope of a block of code, such as within a function definition, loop or conditional statement. Be careful not to mix tabs and spaces in your code, as this can cause indentation errors. Here are a few examples to illustrate how indentation works in Python: Example 1: If-else statement In the correct example, the if and else statements are indented to the same level to indicate they are part of the same block of code. ...

Python For Beginners - Python Variables & Comments

Python Variables In Python, variables are used to store data values. A variable can hold different types of data, such as numbers, strings, or booleans. Here's an example of how to create and use variables in Python: # Integer variable num1 = 5 num2 = 10 sum = num1 + num2 print(sum)  # Output: 15 # String variable name = "John" print("My name is " + name)  # Output: My name is John # Boolean variable is_sunny = True if is_sunny:     print("It's a sunny day!")  # Output: It's a sunny day! else:     print("It's a cloudy day.") In the above example, we first create two integer variables num1 and num2, then we create a variable called sum which holds the sum of num1 and num2. We then print the value of sum using the print() function. Next, we create a string variable name and concatenate it with a string literal using the + operator. Finally, we create a boolean variable is sunny and use it in an if-else statement to print whether it...