Python Tutorial Notes
Variable Type
07/14/2024
A delivery man knocks at your door, carrying a big box for your obsessed shopping sister. As a
bystander, you don't know what's inside. It could be a book, a skincare product, or anything else.
This box represents the variable itself, and its contents represent the values stored within the
variable. Just like you can't predict what's inside the box until you open it, the type of variable
determines what kind of values it can hold. Here's a breakdown:
1. Integer ('int'): This box holds whole numbers without fractions.
2. Float ('float'): Here, you find numbers with fractions.
3. String ('str'): This one includes characters, such as text messages or names enclosed in quotes ("
") or (' ').
4. Boolean ('bool'): In this box, you find logical values representing true or false statements.
5. List ('list'): Imagine a box that organizes items in order; it can contain different types.
6. Dictionary ('dict'): Pairs of keys (variables) and their associated values, defined by curly braces
({}).
User Input
It's a collection of information about a user, which may include different types of values. We use int
or float if the value is a number, and float for both with and without fractions.
Python Tutorial Notes
Conditionals
07/15/2024
Conditions
Programs use conditions to make decisions based on criteria such as data or user input.
Execution Flow
In if-else or if-elif-else statements, the first true condition's block executes, and subsequent blocks
are skipped.
Organizing Conditions
Arrange if-elif-else statements from specific to general for logical order.
Comparison Operators
Operators include:
- == for equal to
- != for not equal to
- > for greater than
- >= for greater than or equal to
Indentation
Crucial in Python to define code blocks, ensuring statements like print are executed correctly.
Python Tutorial Notes
Finding the Minimum & Maximum Number in a List
07/18/2024
Finding the Minimum Number
To find the minimum number in a list, use the min() function. Here is an example:
numbers = [3, 1, 4, 1, 5, 9]
print(min(numbers))
# Output >>> 1
Finding the Maximum Number
To find the maximum number in a list, use the max() function. Here is an example:
numbers = [3, 1, 4, 1, 5, 9]
print(max(numbers))
# Output >>> 9
Checking if an Element Exists in a List
Use the in keyword. Here is an example:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
# Output >>> True
print("orange" in fruits)
Python Tutorial Notes
# Output >>> False
Adding One List to Another List
To concatenate two lists, use the + operator. Example:
list1 = [1, 2, 3, 4, 5, 6, 7]
list2 = [0, -1, -2, -3]
combined_list = list1 + list2
print(combined_list)
# Output >>> [1, 2, 3, 4, 5, 6, 7, 0, -1, -2, -3]
Removing the Last Element from a List
To remove it, use the pop() method. Example:
numbers = [1, 2, 3, 4, 5, 6]
numbers.pop()
# Output >>> 6
print(numbers)
# Output >>> [1, 2, 3, 4, 5]
Combining All Concepts
Here's a combined example to demonstrate all the concepts together:
Python Tutorial Notes
# List of numbers
numbers = [3, 1, 4, 1, 5, 9]
# Find the minimum number
min_number = min(numbers)
print("Minimum number:", min_number)
# Check if an element exists in the list
is_three_in_list = 3 in numbers
print("Is 3 in the list?", is_three_in_list)
# Concatenate two lists
additional_numbers = [2, 6, 5]
combined_numbers = numbers + additional_numbers
print("Combined list:", combined_numbers)
# Remove the last element
combined_numbers.pop()
print("List after removing the last element:", combined_numbers)
Output of the Combined Example
Minimum number: 1
Is 3 in the list? True
Combined list: [3, 1, 4, 1, 5, 9, 2, 6, 5]
Python Tutorial Notes
List after removing the last element: [3, 1, 4, 1, 5, 9, 2, 6]
ToTo
To sort
sort
sort aa list
alistlist ofof
of numbers
numbers
numbers inin
in Python,
Python,
Python, you
youyou can
can can use
use
use the
thethe sort()
sort()
sort() method.
method.
method. Let's
Let's
Let's correct and
correct
correct and
and
refine
refine
refine your
youryour example
example
example to
to to demonstrate
demonstrate
demonstrate how
howhow to
to to do
dodo this
this
this properly.Here's
properly.Here's
properly.Here's the
thethe step-by-step
step-by-step
step-by-step
To sort
process:Define a
process:Define list of
thenumbers
the list of in Python,
numbers.Use you
the can use
sort() the
method to sort the list in ascending
process:Define the listlist
of of numbers.Use
numbers.Use the the sort()
sort() method
method to to sort
sort thethelistlist
in in ascending
ascending
sort()
order.Printmethod.
order.Print the the Let's
sorted
sorted correct
list.Example#and
list.Example# refine
Defineyour
Define theexample
thelist of numbers
order.Print the sorted list.Example# Define the listlist
of of numbers
numbers
to demonstrate
numbers
numbers = [-1,
= [-1, -2, how
-5, 6,to do
100, this
10, properly.Here's
15] the
numbers = [-1, -2,-2,
-5,-5, 6, 6,100,100,10,10,15]15]
step-by-step process:Define the list of numbers.Use
the sort() method to sort the list in ascending
## Sort
#Sort
Sortthe
thethe list
list listinin
in ascending
ascending
ascending order
order
order
order.Print the sorted list.Example# Define the list of
numbers.sort()
numbers.sort()
numbers.sort()
numbers
numbers =sorted
[-1, -2, -5, 6, 100, 10, 15]
## Print
#Print
Printthe
thethe sorted
sorted list
listlist
print(numbers)Explanationnumbers.sort()
print(numbers)Explanationnumbers.sort()
print(numbers)Explanationnumbers.sort() sorts
sorts
sorts the
the thelist numbers
list
list numbers
numbers inin
in place,
place,
place, meaning
meaning
meaning itit it
# Sort the
changes
changes the the list in ascending
original
original list
listto theorder
changes the original list to sorted order.The print(numbers) statement will then
numbers.sort()
output the sorted list.
# Print the sorted list
print(numbers)Explanationnumbers.sort() sorts the list
numbers in place, meaning it changes the original list
to Important Notes