Unparsing Python AST Objects

Learning Python Data Types: A Simple Guide for Beginners

Python has several built-in data types that you can use in your code. These include numbers (integers and floats), strings, lists, tuples, dictionaries, sets, and booleans. Let’s see this up close at each one!

Numbers: Integers and Floats
In Python, integers are whole numbers with no decimal points. For example, 123 is an integer. If you want to use decimals in your code, you can create floating-point numbers (also known as floats). These have a decimal point somewhere in the number. For instance, 3.14 is a float.

To get the data type of any object, you can use the built-in `type()` function:

# This script demonstrates how to use the built-in `type()` function to get the data type of an object.

# First, we assign the integer value 5 to the variable x.
x = 5 # x is an integer

# Then, we use the `type()` function to get the data type of x and print it to the console.
print(type(x)) # Output: <class 'int'> (this shows that x is an integer)

# Next, we assign the float value 3.14 to the variable y.
y = 3.14 # y is a float

# Again, we use the `type()` function to get the data type of y and print it to the console.
print(type(y)) # Output: <class 'float'> (this shows that y is a float)

Setting the Data Type: If you want to specify a data type, you can use constructor functions for each one. For example, if you want to create an integer with the value of 5, you would write `int(5)`. Here’s what that looks like in code:

# Setting the Data Type: If you want to specify a data type, you can use constructor functions for each one. 
# For example, if you want to create an integer with the value of 5, you would write `int(5)`. 
# Here's what that looks like in code:

# Creating an integer with the value of 5 using the int constructor function
x = int(5) # x is now equal to 5 (an integer)

# Printing the data type of x using the type() function
print(type(x)) # Output: <class 'int'>

Strings
A string is a sequence of characters. You can create strings by enclosing them in single or double quotes, like this:

# Define variables for name and age
name = "John" # Assigns the string "John" to the variable name
age = 30 # Assigns the integer 30 to the variable age

# Print a string with the name and age variables
print("My name is " + name + ". I am " + str(age) + " years old.") # Output: My name is John. I am 30 years old. 
# The str() function converts the integer age into a string so it can be concatenated with the other strings in the print statement.

Lists and Tuples
A list is a collection of items that can be accessed using an index (like in math). A tuple works the same way, but it’s immutable (meaning you can’t change its contents once it’s created). Here are some examples:

# Creating a list of fruits
fruits = ["apple", "banana", "cherry"]

# Outputting the type of the fruits list
print(type(fruits)) # Output: <class 'list'>

# Creating a tuple of colors
colors = ("red", "green", "blue")

# Outputting the type of the colors tuple
print(type(colors)) # Output: <class 'tuple'>

Dictionaries and Sets
A dictionary is an unordered collection of key-value pairs. A set is an unordered collection of unique values (no duplicates allowed). Here’s what they look like in code:

# Creating a dictionary with fruit prices
fruit_prices = {"apple": 1.0, "banana": 2.5} # Dictionary with fruit names as keys and their corresponding prices as values
print(type(fruit_prices)) # Output: <class 'dict'> - Prints the type of the variable "fruit_prices", which is a dictionary

# Creating a set of numbers
numbers = {3, 7, 9} # Set of numbers with no duplicates
print(type(numbers)) # Output: <class 'set'> - Prints the type of the variable "numbers", which is a set

Booleans
A boolean is either True or False. Here’s an example:

# Booleans
# A boolean is either True or False. Here's an example:

# Define a variable "is_cool" and assign it a boolean value of True
is_cool = True

# Print the type of the variable "is_cool" using the type() function
print(type(is_cool)) # Output: <class 'bool'>

That’s it! These are the basic data types in Python, and they should be enough to get you started.

SICORPS