python_catchup_2024/1_variables.py
2024-11-08 19:30:32 +00:00

75 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
In this example, we'll cover the fundamental variable types in Python and introduce the basics of functions.
Key Concepts:
**Variables**: Variables store data that can be changed while the program is running.
**Dynamic Typing**: Python is a dynamically-typed language, which means you don't need to specify
the data type of a variable explicitly (unlike statically-typed languages like C or Java, where you do).
- Example in Java: `int number = 1;`
- In Python: `number = 1` (Python automatically understands that `number` is an integer).
"""
# The following are common variable types in Python. This list is not exhaustive, but it covers the basics.
# Integer: An integer (int) represents a whole number, with no decimal point.
number = 1
# Float: A floating point number (float) represents a number with a decimal point.
number_float = 1.1
# String: A string (str) represents a sequence of characters, typically used for text.
text = "Hello, World!"
# Boolean: A boolean (bool) represents a logical value, either True or False.
boolean = True
# List: A list stores a collection of values in a specific order, and the values can be of any type.
# Lists are mutable, meaning they can be changed after theyre created (e.g., adding or removing items).
a_list = ["Apple", 64, 1.1, True]
# Tuple: A tuple is similar to a list in that it can store multiple values, but it is immutable,
# meaning once its created, it cannot be changed.
a_tuple = ("Apple", 54, 11, 1.2, False)
#dict: stores a value with a key
# Dictionaries (dict) are extremely useful, they allow you to map a name (key) to a value.
# they are mutable, meaning they can be changed after theyre created (e.g., adding or removing items).
dictionary = {
"Jan" : "January",
"Feb" : "February",
"Mar" : "March",
"Apr" : "April",
"May" : "May",
"Jun" : "June",
"Jul" : "July",
"Aug" : "August",
"Sep" : "September",
"Oct" : "October",
"Nov" : "November",
"Dec" : "December"
}
"""
A function allows you to define a reusable block of code that performs a specific task.
Functions can:
- Take input (called parameters or arguments)
- Perform operations with that input
- Return a result, or simply perform an action (like printing something)
Below is a function called `show_type` that takes in one parameter, `var`, and prints its data type.
"""
def show_type(var):
"""Prints the data type of the variable passed to it."""
print(f"The type of {var} is {type(var)}")
# Let's test our function with the variables defined above.
show_type(number) # Expected output: <class 'int'>
show_type(number_float) # Expected output: <class 'float'>
show_type(text) # Expected output: <class 'str'>
show_type(boolean) # Expected output: <class 'bool'>
show_type(a_list) # Expected output: <class 'list'>
show_type(a_tuple) # Expected output: <class 'tuple'>