← Back to Articles
# What is an Array?
Variables and Arrays
Jamie Z 2024-01-29Learning Goals
By the end of this lesson I will be able to:
- Understand what variables and arrays are
- Understand how to use variables and arrays in python
Python Syntax
Indentation:
- Indentation refers to the spaces at the beginning of a code line.
- Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
- Python uses indentation to indicate a block of code.
if 5>2:
print("5 is larger than 2")
Comments:
- Python has commenting capability for the purpose of in-code documentation.
- Comments start with a #, and Python will render the rest of the line as a comment:
#this is a comment
print("hellowowowow")
Data types
In python there are several built in data types that you can use to store different types of data
- Integer: A whole number such as 1, 110 or 100
- Float: A decimal number such as 3.14 or 0.01
- String: A sequence of characters, such as “hello world”
- Boolean: A value that can be either True or False
We will be able to store these different types of data in an array or list
Variables
- A variable that can change, or be change, during the course of a program’s execution
x = 5
print(x)
Playing With Variables
Example
```python x = 5 y=10 #add variables z = x+y print(z) #subtract variables z = y-x print(z) #multiply variables z=x*y print(z) #divide variables z = y/x print(int(z)) ```- Variables are used to store information. But one variable can only store one piece of information at a time. However, you can use an Array to store lots of information.
- Array start at index 0
- To create an empty array, choose a name for your array and assign it a pair of empty square brackets like this:
Backpack = []
- Arrays can also start with data being held, like the example below:
Backpack = ['water bottle', "umbrella", 'apple']
- The things that an array stores are called elements. The Backpack array has three elements.
There are four main functions you will do with arrays this Term:
- Create arrays
- Add an element to the array
- Remove an element from the array
- Sort the Array
Example
```python # Create an array fruits = ['apple', 'banana', 'cherry']Add an element to the array
fruits.append('date') print(fruits)
Remove an element from the array
fruits.pop(1) print(fruits)
Sort the array
fruits.sort() print(fruits)
</details>
# Activity
Research and create your own python program that:
- Asks for your name
- Accepts a user input
- Stores the name
- Outputs it back to the user in a string that says “Hello, {name}. Welcome to your first Python Program.”
```python
name = input("What is your name?")
print(f"Hello, {name}. Welcome to your first Python Program")