Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Get hands-on with Python lists β master storing and managing of multiple items in one place. β If you are an absolute beginner you can glance through previous issues as well β A "list" is a datatype that can store multiple values as one variable. Yes you can stuff numbers, strings, anything together β π What does it look like? Reminder: print() is often used to display output
In [1]:
# List is represented by []
# 2,7 & 4 are "items" of the list numbers = [2, 7, 4] print(numbers)
Out [1]: [2, 7, 4]
β ποΈ As I said anything can be stuffed into a list
In [2]:
mix_list = ["Pandas", 2, "Is", 10.0]
print(mix_list)
Out [2]: ['Pandas', 2, 'Is', 10.0]
β β Verify using "type" if its the right data type
In [3]:
type(mix_list)
Out [3]: list
β π€― There can be a list of lists too
In [4]:
super_list = [
[1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(super_list)
Out [4]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
β π Unlike strings, length of list is the number of items in the list
In [5]:
len(mix_list)
Out [5]: 4
β π Going one level deeper β πΆββοΈπΆββοΈ Similar to what we saw for strings yesterday, list items also have positions starting 0
In [6]:# Access 1st item
mix_list[0]
Out [6]: Pandas
β
In [7]:# Access 2nd
mix_list[1]
Out [7]: 2
β
In [8]:
# Get first 2 items - 0,1
mix_list[:2]
Out [8]: ['Pandas', 2]
β
In [9]:
# Access last element
mix_list[-1]
Out [9]: 10.0
β π οΈ Tweaking Lists β π Add 1 element at the end
In [10]:
mix_list.append("awesome")
mix_list
Out [10]: ['Pandas', 2, 'Is', 10.0, 'awesome']
β π Add 1 element at a specific position
In [11]:
# "insert" 9 at 2nd position
# Reminder 1 below implies 2nd position mix_list.insert(1, 9) mix_list
Out [11]: ['Pandas', 9, 2, 'Is', 10.0, 'awesome']
β βοΈ Remove particular value
In [12]:
# Remove 9
mix_list.remove(9) mix_list
Out [12]: ['Pandas', 2, 'Is', 10.0, 'awesome']
β βοΈ Remove from specific position
In [13]:
# Remove by position
# 1 below means 2nd position mix_list.pop(1) mix_list
Out [13]: ['Pandas', 'Is', 10.0, 'awesome']
β π To Turnaround
In [14]:
mix_list.reverse()
mix_list
Out [14]: ['awesome', 10.0, 'Is', 'Pandas']
β πΆ Sorting Lists β Should be of same datatype.
In [15]:
numbers = [2, 7, 4]
numbers.sort() numbers
Out [15]: [2, 4, 7]
β
In [16]:
alphabet_list = [
"Please",
"Share",
"This",
"Newsletter"]
alphabet_list
Out [16]: ['Please', 'Share', 'This', 'Newsletter']
In [17]:
alphabet_list.sort()
alphabet_list
Out [17]: ['Newsletter', 'Please', 'Share', 'This']
βοΈ Why mix_list cannot be sorted and will give error. Reply if you know the answer. β Use "reverse = True" for sorting in descending order
In [18]:
alphabet_list.sort(reverse=True)
alphabet_list
Out [18]: ['This', 'Share', 'Please', 'Newsletter']
β π€π€ Simply add or use extend function to join lists
In [19]:
# alphabet_list.extend(numbers) will also give same result
alphabet_list + numbers
Out [19]: ['This', 'Share', 'Please', 'Newsletter', 2, 4, 7]
β Coming up tomorrow: Concept of Immutability (simple but important concept) and dictionaries. β βπ£ If you liked it, please share it with anyone who will find it useful and share your feedback below πΌ β
|
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Letβs dive into Python "Dictionaries", the toolbox for storing data with labels and values. If you are an absolute beginner you can glance through previous issues as well Think of Python Dictionaries like a real world dictionary: you look up a word (key) and find its meaning (value). Data is basically stored as key-value pair. Use when you want to connect a label (like a name) to information (like...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Dive into Python strings β learn how to create, manipulate, and explore them. "String" is any sequence of characters (letters, numbers, symbols etc.) in quotes. Can be represented in single βοΈ or double quotes βοΈ In [1]: # String in single quotes 'I love Pandas Daily' Out [1]: 'I love Pandas Daily' In [2]: # String in double quotes "I love Pandas Daily" Out [2]: 'I love Pandas Daily' β©οΈ Slight...
Welcome to the very first issue of Pandas Daily β your 5 minute daily dose of Python. Today: Understand some of Pythonβs data types with examples and clean output. βData Typesβ help the computer understand what you're working withβnumbers, text, or something else. Common Data Types 1. Numbers Reminder: As seen below, "In [1]" is the code input we will type in and "Out [1]" is the output of code once executed In [1]: # Integers (anything starting with '#' is a comment and not executed) 8 Out...