My journey through learning Python: Day 4 to 5
Hey, you came back, fantastic! π€©
In this article, we'll talk about the random
Python module, about lists and loops. So, take a seat, grab your cup of tea and here we go.
Day 4: Randomisation and Python Lists
Randomisation
Randomisation is a powerful concept in computer programming that allows for the generation of unpredictable and diverse outcomes. By introducing randomness into your programs, you can add a layer of complexity and variety that can make them more interesting and useful. In Python, randomisation can be achieved using the built-in library random
.
Here is an example of how to generate a random number in Python:
import random
# Generate a random number between 1 and 10
random_number = random.randint(1, 10)
# Print the random number
print(f'The random number is: {random_number}')
In this example, we first import the random
module using the import
statement. We then use the randint
function from the random
module to generate a random integer between 1 and 10 (inclusive). Finally, we print the random number using the print
statement.
Each time we run this code, a different random number between 1 and 10 will be generated and printed to the console.
Lists
Lists are an essential data structure in Python, providing a way to store and manipulate collections of data. A list is an ordered collection of elements, which can be of any data type - integers, strings, floating-point numbers, or even other lists (called nested lists).
Creating a list in Python is simple. You can define a list by enclosing a comma-separated sequence of values in square brackets, like this:
my_list = [1, 2, 3, 4, 5]
You can also create an empty list using square brackets with no values inside:
empty_list = []
Once you have created a list, you can access individual elements using their index, which is their position inside the list. In the introduction, we said that a list is ordered. That means that the elements are in a certain order in the list and that order doesn't change.
Note: In Python, lists are zero-indexed, meaning that the first element has an index of 0
, the second element has an index of 1
, and so on. You can access an element by specifying its index in square brackets, like this:
first_element = my_list[0]
second_element = my_list[1]
You can also modify the elements of a list by assigning a new value to a specific index, like this:
my_list = [0, 1, 2]
my_list[0] = 100
print(my_list)
# [100, 1, 2]
Lists also provide a range of built-in methods for manipulating their contents. For example, you can add elements to a list using the append()
method:
my_list = [0, 1, 2]
my_list.append(6)
print(my_list)
# [0, 1, 2, 6]
Note that the appened element is at the end of the list.
You can also remove elements using the remove()
method:
my_list = [0, 1, 2]
my_list.remove(0)
print(my_list)
# [1, 2]
You can see all the methods about lists in the Python documentation: right here
Day 5: Python for
Loops
A for
loop in Python is a control flow statement that allows you to iterate over a sequence (such as a list
, tuple
, or string
) or other iterable objects (such as range
objects) and execute a block of code for each item in the sequence.
Here is the basic syntax of a for loop in Python:
for variable in sequence:
# code block to be executed
In the above code, variable
is a variable that is updated at each iteration of the loop, and sequence
is the sequence or iterable object over which the loop is iterating.
Here is an example of a for loop that iterates over a list of numbers and prints out each number:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
This would output:
1
2
3
4
5
You can also use the built-in range()
function to generate a sequence of numbers to iterate over. For example:
for i in range(5):
print(i)
This would output:
0
1
2
3
4
Note that just like list
indexes, the range
function starts at 0
, unless you specifies the starting point, like :
for i in range(1, 5):
print(i)
This one would output:
1
2
3
4
In the next article, we'll talk about functions, a big piece of Python! So, stay with me and see you soon. π