Introduction to Python
This notebook will cover basic programming concepts and how to implement them in Python.
Intro to Python: Syntax Crash Course
Welcome to Part 1 of our Intro to Python workshop! In this notebook, we'll be covering the basics of Python syntax and how to use it to write simple programs. Whether you're a complete beginner to programming or have experience in other languages, this crash coruse will provide you with a solid foundation in Python syntax.
What is Python?
Python is a popular programming language used for a variety of applications, including web development, data analysis, and artificial intelligence. It's known for its readability, simplicity, and ease of use, making it a great choice for beginners and experienced programmers alike.
In this notebook, we'll start with the fundamentals of Python syntax, including variables, data types, and basic operations. We'll also cover control structures such as conditionals and loops, as well as functions and modules. By the end of this module, you'll be able to write simple Python programs and have a good understanding of the core concepts of Python programming.
A Note About Notebooks
Jupyter notebooks are an interactive environment for writing and running code. They allow you to write code in small, manageable chunks called "cells", which can be executed independently. This makes it easy to experiment with code, debug errors, and visualize data. To run a cell in a Jupyter notebook, you can click on the cell to select it, and then press the "Run" button in the toolbar at the top of the notebook. Alternatively, you can use the keyboard shortcut "Shift + Enter" to run the cell. When you run a cell, any output generated by the code will be displayed below the cell. This can include text output, plots, tables, and other visualizations.
Jupyter notebooks also allow you to write formatted text using a markup language called Markdown. You can switch between code cells and Markdown cells by selecting the appropriate cell type from the dropdown menu in the toolbar.
Hello, World!
Python is a high-level, interpreted programming language. This means that Python code is executed by an interpreter, which reads and executes Python code one line at a time, rather than compiling the entire code into a binary executable. This makes Python code easy to write, read, and understand.
Python code is typically written in a text editor or an integrated development environment (IDE). There are many popular IDEs available for Python, including PyCharm, Spyder, and Visual Studio Code. In this workshop, we'll be using Jupyter notebooks, which provide an interactive environment for writing and running Python code.
Python code is structured using whitespace, rather than using curly braces or other delimiters like many other programming languages. This means that the indentation of code is significant in Python, and code blocks are defined by the level of indentation.
Let's start by looking at a simple example of Python code:
This code will output the string "Hello, world!" to the console when executed. As you can see, the syntax of Python is very straightforward and easy to understand.
In the following sections, we'll explore the different elements of Python syntax, including variables, data types, operators, control structures, functions, and modules.
Variables and Data Types
In Python, a variable is a named storage location that can hold a value. Variables are used to store data that may change during the execution of a program.
To declare a variable in Python, you simply assign a value to a name using the equals sign (=). For example:
In this example, we've declared a variable called message and assigned it the value "Hello, world!". Note that we don't need to declare the data type of the variable explicitly - Python will automatically determine the data type based on the value we assign to it.
Python supports several basic data types, including:
- Integers: whole numbers, such as 1, 2, -3, and 0.
- Floats: decimal numbers, such as 3.14, -0.5, and 2.0.
- Strings: text, enclosed in quotation marks, such as "Hello" or "Python".
- Booleans: True or False, used for logical operations.
To determine the data type of a variable in Python, you can use the type() function. For example:
This code will output the type of the variable message, which is <class 'str'> (short for "string").
You can also convert between data types using type casting. For example, to convert a string to an integer, you can use the int() function:
Variable Naming
In Python, it's important to choose descriptive names for your variables that accurately represent their purpose in your code. Variable names can include letters, numbers, and underscores, but cannot start with a number. It's also a good practice to follow a naming convention for your variables, such as using lowercase letters and underscores to separate words (e.g. message_count).
In the following sections, we'll explore the different data types in more detail, and look at how to perform operations on them.
Exercise 1: About Me
Create a variable called my_age and assign it the value of your age as an integer. Then, create a variable called my_name and assign it the value of your name as a string. Finally, print out the type of each variable.
Enter your answer in the cell below:
Converting Between Datatypes
In Python, you can convert an integer to a decimal (float) using the float() function. For example:
Conversely, you can convert a decimal to an integer using the int() function. This function will truncate the decimal portion of the number, so be careful when using it. For example:
This code will output the integer value 3. Note that the decimal portion of the original number was truncated, not rounded. If you want to round the number before converting it to an integer, you can use the round() function. This code will output the integer value 4:
Exercise:
Create a variable called num_int and assign it the value of 42 as an integer. Then, convert num_int to a decimal and assign it to a new variable called num_float. Next, convert num_float back to an integer and assign it to a new variable called num_int_2. Finally, print out the values of num_int, num_float, and num_int_2
Basic Operators in Python
Operators are symbols or keywords used to perform operations on values or variables. Here are some of the most common operators in Python:
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. Here are the basic arithmetic operators in Python:
| Operator | Description | Example |
|---|---|---|
| + | Addition | 3 + 5 evaluates to 8 |
| - | Subtraction | 7 - 2 evaluates to 5 |
| * | Multiplication | 4 * 6 evaluates to 24 |
| / | Division | 10 / 2 evaluates to 5.0 |
| // | Integer Division | 10 // 3 evaluates to 3 |
| % | Modulus | 10 % 3 evaluates to 1 |
| ** | Exponentiation | 2 ** 3 evaluates to 8 |
Arithmetic operators are used to perform mathematical operations on numeric data types in Python. The basic arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**). In addition to these basic operators, there are also two additional arithmetic operators: integer division (//) and modulus (%).
The addition, subtraction, multiplication, and exponentiation operators work as expected. For example, 3 + 5 evaluates to 8, 7 - 2 evaluates to 5, 4 * 6 evaluates to 24, and 2 ** 3 evaluates to 8.
The division operator (/) performs floating-point division, meaning that it returns a decimal number even if the inputs are integers. For example, 10 / 2 evaluates to 5.0, not 5.
The integer division operator (//) performs integer division, which returns the integer portion of the result, discarding any remainder. For example, 10 // 3 evaluates to 3, not 3.33333....
The modulus operator (%) returns the remainder of a division operation. For example, 10 % 3 evaluates to 1, because 10 divided by 3 leaves a remainder of 1.
It's important to note that the order of operations in Python follows the same rules as standard mathematics. In other words, multiplication and division are performed before addition and subtraction, and parentheses can be used to change the order of operations.
Exercise: Area of a Rectangle
Write a program that calculates the area of a rectangle with length 8 and width 5. Print the result.
Exercise: Calculate Order Total
Write a program that calculates the total cost of purchasing 10 items that each cost $5.99. Print the result.
Comparison Operators
Comparison operators are used to compare values. The result of a comparison is a boolean value, either True or False. Here are the comparison operators in Python:
| Operator | Description | Example |
|---|---|---|
| == | Equal | 3 == 5 evaluates to False |
| != | Not equal | 7 != 2 evaluates to True |
| < | Less than | 4 < 6 evaluates to True |
| > | Greater than | 10 > 2 evaluates to True |
| <= | Less than or equal to | 10 <= 3 evaluates to False |
| >= | Greater than or equal to | 2 >= 3 evaluates to False |
Let's see these operators in action:
Each comparison returns a boolean value — either True or False.
It's important to note that the == operator is used to check if two values are equal, whereas the = operator is used to assign a value to a variable. For example:
Notice that x = 5 assigns the value 5 to the variable x, while x == y compares x and y and returns a boolean result.
Exercise
Guess the output of each statement before running the next cell
Logical Operators
Logical operators are used to combine multiple boolean expressions to create more complex conditions. Here are the basic logical operators in Python:
| Operator | Description | Example |
|---|---|---|
| and | True if both expressions are True | 3 < 5 and 7 > 2 evaluates to True |
| or | True if at least one expression is True | 3 > 5 or 7 > 2 evaluates to True |
| not | True if the expression is False | not(3 < 5) evaluates to False |
The and operator returns True if both expressions it is combining are True. If either expression is False, the result of the and operator is False. For example:
In this example, the and operator combines two comparisons. The first print shows True because both x < y and y < z are True. The second print shows False because y > z is False.
The or operator returns True if at least one of the expressions it is combining is True. If both expressions are False, the result of the or operator is False. For example:
In this example, the or operator combines two comparisons. The first print shows True because y < z is True (even though x > y is False). The second print shows False because both conditions are False.
The not operator is used to negate a boolean expression. If the expression is True, the not operator returns False. If the expression is False, the not operator returns True. For example:
In this example, not reverses the boolean value. not (x < y) evaluates to False because x < y is True, and not (x > y) evaluates to True because x > y is False.
Conditional Statements
Conditional statements allow you to control the flow of your program based on certain conditions. In Python, conditional statements use the if, elif, and else keywords.
The if statement is used to test a condition. If the condition is true, the code inside the if block is executed. If the condition is false, the code inside the if block is skipped. Here's the syntax for an if statement:
In this example, the if statement checks if x is greater than 0. Since x is 5, which is greater than 0, the code inside the if block is executed and "x is positive" is printed.
The if...else statement is used to execute one block of code if a condition is true, and another block of code if the condition is false. Here's the syntax for an if...else statement:
In this example, the if statement checks if x is greater than 0. Since x is -5, which is not greater than 0, the code inside the else block is executed and "x is not positive" is printed.
The if...elif...else statement is used to execute one block of code if a condition is true, another block of code if a different condition is true, and a final block of code if all conditions are false. Here's the syntax for an if...elif...else statement:
In this example, the if statement checks if x is greater than 0. Since x is not greater than 0, the elif statement checks if x is less than 0. Since x is not less than 0 either, the else block is executed and "x is zero" is printed.
Exercise: Temperature Converter
Write a program that converts temperatures between Fahrenheit and Celsius. The program should prompt the user to enter a temperature and a unit of measurement (either "F" for Fahrenheit or "C" for Celsius), and then convert the temperature to the other unit.
Here are the conversion formulas:
- To convert Fahrenheit to Celsius: subtract 32 from the temperature, then multiply the result by 5/9
- To convert Celsius to Fahrenheit: multiply the temperature by 9/5, then add 32 Your program should use conditional statements to determine which conversion formula to use based on the unit of measurement entered by the user.
Data Structures in Python
A data structure is a way of organizing and storing data so that it can be accessed and used efficiently. Python provides several built-in data structures, including lists, tuples, and dictionaries. In this section, we'll cover each of these data structures in detail, including how to create and modify them, and some common use cases
Lists
In Python, a list is a collection of items that are ordered and changeable. Lists are denoted by square brackets [] and can contain items of any data type, including other lists.
To create a new list, simply enclose a comma-separated sequence of values in square brackets. For example:
You can access individual items in a list using square bracket notation and the item's index. Python uses zero-based indexing, meaning that the first item in a list has an index of 0.
You can also access a range of items in a list using slicing notation, which consists of two indices separated by a colon.
Lists are mutable, which means that you can change their contents by assigning new values to individual items or to slices of the list.
List Methods Python provides several built-in methods for working with lists. Here are a few examples:
Exercise
Use the vehicles list to answer the following questions:
- Print the first vehicle in the list.
- Print the last vehicle in the list.
- Print the third vehicle in the list.
- Print the first 4 vehicles in the list.
- Print the last 3 vehicles in the list.
Tuples
A tuple is a collection of ordered, immutable elements. This means that once you create a tuple, you cannot modify it. Tuples are created using parentheses () and commas , to separate the elements. Here's an example of creating a tuple:
Once a tuple is created, you can access its elements using indexing, just like with lists. However, because tuples are immutable, you cannot change their elements. If you try to assign a new value to an element of a tuple, you'll get a TypeError.
Dictionaries
A dictionary is a collection of key-value pairs. Each key in a dictionary is associated with a value, and you can use the key to access the value. Dictionaries are created using curly braces {} and colons : to separate keys and values. Here's an example of creating a dictionary:
To access a value in a dictionary, you can use its key as an index:
You can also change the value associated with a key by assigning a new value to that key:
Dictionaries are a powerful and flexible data structure, and they're commonly used in Python programs. We'll cover more advanced dictionary operations later in the workshop.
Control Flow
Control flow statements are used to control the order in which statements are executed in a program. They allow you to execute different code blocks based on certain conditions, or to repeat code blocks multiple times. The most commonly used control flow statements in Python are:
| Control Structure | Syntax | Description |
|---|---|---|
| if statement | if condition:statement(s) | Executes the code block under the condition, only if the condition is true. |
| if-else statement | if condition:statement(s)else:statement(s) | Executes the code block under the if statement if the condition is true, otherwise it executes the code block under the else statement. |
| for loop | for element in iterable:statement(s) | Loops through each element in an iterable object, executing the code block each time. |
| while loop | while condition:statement(s) | Loops through a block of code as long as the condition specified is true. |
if statements
As seen above, this is the structure of an if statement that uses the elif and else blocks:
In this example, the first condition (x > 10) is false, so the elif statement is checked. The second condition (x > 5) is also false, so the else statement is executed and the message "x is less than or equal to 5" is printed.
Loops in Python
Loops are used to repeat a block of code multiple times. They are helpful when you want to process items in a list, repeat an action, or run code until a condition changes.
Python has two main types of loops:
- for loops: repeat over a sequence of values
- while loops: repeat as long as a condition is true
Loops make it easier to write efficient and readable code when working with repeated tasks.
For Loops
A for loop is used to execute a code block repeatedly for a set number of times or for each element in a sequence. Here's the syntax of a for loop:
for variable in sequence:
# code block to execute for each iteration
The variable is assigned the value of each element in the sequence in turn, and the code block is executed once for each element. For example, here's a for loop that prints the numbers from 1 to 5:
If you run this code, it will output the numbers 1 through 5.
You can use break and continue statements to control the behavior of a for loop. The break statement stops the loop entirely, while the continue statement skips the rest of the code block for the current iteration and moves on to the next element in the sequence.
In this example, the loop will iterate through each fruit in the fruits list. When it reaches the element "cherry", the if statement will evaluate to True and the break statement will be executed, causing the loop to terminate early. The final output will only include "apple" and "banana".
While Loops
while loops are used to repeatedly execute a block of code as long as a certain condition is met. The syntax of a while loop is as follows:
while condition:
statement(s)
The loop continues to execute the block of code under the while statement as long as the condition is true. Once the condition becomes false, the loop terminates and the program continues with the code that appears after the loop.
Here's an example of a while loop that counts from 0 to 4:
In this example, the while loop continues to execute the block of code as long as the i < 5 condition is true. The loop will execute five times, printing the values 0 through 4 to the console.
One important thing to note when using while loops is that if the condition never becomes false, the loop will continue to execute indefinitely. This is known as an "infinite loop" and can cause your program to crash or become unresponsive. It's important to ensure that the condition will eventually become false, or to use a break statement to exit the loop under certain conditions.
Functions
In Python, a function is a block of code that performs a specific task. Functions are useful because they allow us to break our code into smaller, more manageable pieces, and they can be reused throughout our program. A function definition starts with the def keyword, followed by a name and any parameters inside parentheses. The body of the function is indented, and it only runs when the function is called.
Functions are especially helpful when you want to avoid repeating the same code in multiple places. For example, instead of writing the same greeting logic over and over, you can define it once and call it whenever you need it. A parameter is a placeholder for data the function will use, while an argument is the actual value you pass in when you call the function.
You can also use the return keyword to send a value back to the caller. This is useful when you want a function to compute something and use that result later in your program.
Defining a Function
To define a function in Python, we use the def keyword followed by the function name and any required arguments in parentheses. The function body is then indented beneath the function definition. Here's a simple example:
In this example, we define a function called greet that takes one argument called name. The function body simply prints a greeting message to the console that includes the name argument.
Calling a Function
To call a function in Python, we use its name followed by any required arguments in parentheses. Here's an example of calling the greet function defined above:
Returning a Value from a Function
In addition to printing output to the console, functions can also return a value to the caller using the return keyword. Here's an example:
In this example, we define a function called square that takes one argument called x. The function body computes the square of x using the * operator and returns the result using the return keyword.
We can call the square function and store its result in a variable like this:
Default Parameters
Sometimes you want a function to use a sensible default value if the caller does not provide one. You can do this by assigning a default in the function definition. Default values make functions easier to use while still allowing you to override them when needed.
Conclusion
Great job! You've made it to the end of this Intro to Python workshop. By now, you should have a solid foundation in the basic syntax and concepts of the Python programming language. We started by introducing you to the Jupyter Notebook and then covered variables, data types, arithmetic, comparison and logical operators, control structures, and data structures such as lists, tuples, and dictionaries.
While this workshop has given you a great start, there's still much more to learn. Python is a powerful and versatile language that is used in a wide range of industries, from web development to data science and machine learning. If you're interested in pursuing a career in programming or just want to continue learning, we encourage you to explore the vast resources available online, including tutorials, documentation, and open-source projects.
Thank you for joining us on this introductory journey into the world of Python programming. We hope that you found this workshop informative and engaging, and we wish you the best of luck in your future programming endeavors!
Continue learning
If you've taken an Intro to Python module, then this challenge will help you apply what you've learned by using the Turtle program to visualize some of these programming concepts
Optional: a full free Python course from freeCodeCamp for extra practice.