Introduction To Quantitative Data Analysis in Python (I)
Introduction To Quantitative Data Analysis in Python (I)
2 Python Variables
Variables are containers for storing data values. Rules for Python variables:
• A variable name must start with a letter (conventionally low case letters)
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and
_)
• Variable names are case-sensitive
1
3 Python Data Types
Python has the following data types built-in by default, in these categories:
• Numeric Types: int, float, complex
• Text Type: str
• Sequence Types: list, tuple, range
• Mapping Type: dict
• Set Types: set, frozenset
• Boolean Type: bool
• Binary Types: bytes, bytearray, memoryview
[1]: # Python numbers in three types
i1 = 1 # int
f1 = 2.8 # float
f2 = 3.5e3 # "e" to indicate the power of 10.
z = 3+ 5j # complex
# use the type() funciton to verify the type of any object in Python
print(type(i1))
print(f2)
print(type(z))
<class 'int'>
3500.0
<class 'complex'>
[2]: # String literals in python are surrounded by either single quotation marks,
# or double quotation marks.
# Slicing a string by specifying the start index and the end index, separated␣
,→by a colon
2
print("Slicing a string: ", my_str[2:5]) # Get characters from position 2 to 5␣
,→(not included)
# The strip() method removes any whitespace from the beginning or the end
my_str2 = " Hello, World! "
print("Stripping a string: ", my_str2.strip())
# The upper() and lower() method returns the string in upper and lower case,␣
,→respectively
# The format() method takes the passed arguments, formats them, and places them␣
,→in the string
Hello, World!
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
3
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
# Access the list items by referring to the index number (0 refer to the first␣
,→item)
# Negative indexing means beginning from the end (-1 refers to the last item)
print("Access an item in a list: ", my_list[0], my_list[-1])
4
# Use pop() method removes the item of specified index (or the last item if␣
,→index not specified)
my_list.remove("grape")
print("Remove an item from a list:", my_list)
my_list.pop(1)
print("Remove an item from a list", my_list)
5
[4]: # A tuple is a collection which is ordered and unchangeable, or immutable
# Once a tuple is created, you cannot change/add/remove an item
# In Python tuples are written with round brackets
# Create a tuple
my_tuple = ("apple", "banana", "cherry", "durian", "fig", "pear")
print(my_tuple)
# Tuple cannot be changed directly, but you can convert the tuple into a list,
# then change the list, and convert the list back into a tuple.
my_tuple = ("apple", "banana", "cherry")
my_list = list(my_tuple)
my_list[1] = "kiwi"
my_tuple = tuple(my_list)
6
# Check if a specified value is present in a set, by using the 'in' keyword
print("Check if an item in a set:", "banana" in my_set)
# Once a set is created, its items cannot be changed, but new items can be␣
,→added in.
# Use the union() method that returns a new set containing all items from both␣
,→sets,
7
Remove the last item from a set: {'cherry', 'grapes', 'apple', 'mango'}
Length of the set: 4
Join two sets: {'grapes', 'pear', 'apple', 'cherry', 'fig', 'mango'}
Empty a set: set()
my_dict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(my_dict)
# Access the items of a dictionary by referring to its key name, inside square␣
,→brackets
# Loop through both keys and values, by using the items() method
for x, y in my_dict.items():
print("Each item in the dictionary:", x, y)
# Add an item to the dictionary is done by using a new index key and assigning␣
,→a value to it
my_dict["color"] = "red"
print("Add an item to the dictionary:", my_dict)
8
# The pop() method removes the item with the specified key name
my_dict.pop("model")
print("Remove an item from the dictionary:", my_dict)
9
x = complex(1j) # complex
x = list(("apple", "banana", "cherry")) # list
x = tuple(("apple", "banana", "cherry")) # tuple
x = range(6) # range
x = dict(name="John", age=36) # dict
x = set(("apple", "banana", "cherry")) # set
x = frozenset(("apple", "banana", "cherry")) # frozenset
x = bool(5) # bool
4 Function
A function is defined by the keyword def, and can be defined anywhere in Python. It returns the
object in the return statement, typically at the end of the function.
A lambda function is a small anonymous function. A lambda function can take any number of
arguments, but can only have one expression. The power of lambda is better shown when you use
them as an anonymous function inside another function.
[8]: def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
10
# A lambda function that multiplies argument a with argument b and print the␣
,→result:
x = lambda a, b : a * b
print("Calling a lambda function:", x(5, 6))
mydoubler = myfunc(2)
print("Calling a lamdba fuction inside a function: ", mydoubler(11))
Emil Refsnes
Tobias Refsnes
Linus Refsnes
The youngest child is Linus
The youngest child is Linus
His last name is Refsnes
Calling a lambda function: 30
Calling a lamdba fuction inside a function: 22
11