Python Beginner Tutorial - The Basics
Chapter 1 - What is Python
Python is a versatile programming language that can be used for a variety of tasks ranging from web development to data science and AI. Python is also an excellent choice for beginner programmers due to it's easy to read syntax and wide range of functionality. Many people will also find Python extremely useful for automation and other daily tasks.
Chapter 2 - Hello World
Hello world, is often the first line of code that programmers write regardless of what language they use. This can be done just as easily in Python by writing the following code:
By pressing run at the top of your screen, this line of code should output:
This uses an extremely useful function called
print() as well as a string which we will get into later.
Mini Project: Try to replace the text within the print function so that it prints some other text of your choice.
Congratulations! You have successfully written your first Python program!
Chapter 3 - Variables and Data Types
Part 1 - Variables
Another essential part of any program is a variable, which can be used to store and change data that you use in your program. Each variable has two important parts:
- The name, which is what you call your variable and is needed to access it in the future.
- The value, which is the data stored in the variable, and can be classified into four data types.
Part 2 - Data Types
Along with variables, understanding data types is an utmost priority for any new programmer as they will come in handy when writing complex programs. In a nutshell, data types are a way to classify the value in a variable into one of four types:
- Strings - Are used to store text, and must be written with quotes like this:
string = 'Some Text'
- Integers - Are used to store whole numbers such as -1 or 1. To make a variable with an integer you would just type:
integer = 26
- Floating Points - Are used to store numbers with decimals or other rational numbers. This can be created by typing:
float = 1.25
- Boolean - Are used to store
Falsevalues by inputing:
boolean = True
Just for clarification, you can name the variable whatever you want. I just named it to correspond with the data type for simplicity.
Mini Project: Make 4 variables using different data types and give them unique names and values. The solution will be posted below.
sample_string = 'Python is cool' sample_integer = 4 sample_float = 3.24 sample_boolean = True
Chapter 4 - The Print Function
The print function is a function that can be used to print things in the console. It can also be used to print variables and do various other things. When printing variables, you must convert any non-string variables to strings by writing
str(your_variable). I have put an example for reference:
num = 40 print(str(num))
Now, this snippet of code should output:
Using this you can also convert the other data types such as floats and booleans
Another important aspect of print is concatenating string within the
print(). For those who may not know, concatenating is used to join two different variables or strings together in Python. This can be done by using
+ along with two strings or variables that are strings. Below is an example where I concatenate a string and variable:
num = 35 print(str(num) + 'is a number!')
This code should output:
35 is a number!
When concatenating, keep in mind to convert any non-string variables to a string by using
Mini Project: Make two variables(1 should be non-string) and print them by concatenating.
decimal = 3.21 text = 'is a decimal' print(str(decimal) + text)
Chapter 5 - Arithmetic Operations
Arithmetic operations are used to perform math operations in python. They can be used on integers and floats, though when adding them you must convert the integer to a float by using
float(). There are 5 major operations which are as follows:
- Addition = +
- Subtraction = -
- Multiplication = *
- Division = /
- Modulo = %
I'm assuming that most of you will be familiar with all of them apart from the modulo operator which is used to find the remainder of a division equation.
num1 = 10 num2 = 7 res = num1 % num2 print(str(res))
This code should output:
This happens due to the fact that 10/7 leaves the remainder of 3 if you don't divide for a decimal. This is extremely useful for checking if 2 numbers divide evenly, which can be used in a variety of programs.
Another important thing to mention is that you must convert the result of an operation to a string before printing because it will always be an integer or float
Mini Project: Make a simple calculator that performs and operation on two variables and prints the result.
num1 = 5 num2 = 7 res = num1 * num2 print(str(res))
Chapeter 6 - Input Function
In addition to variables, the input function can be used to add a custom input to a variable through the console. This can be done by typing:
variable_name = input('Enter your data: '). In that little snippet of code, you can see that you write
input() for the value and can also write text within it as an instruction.
An example of this being used is:
user_input = input('Enter your text: ') print(user_input)
This should output:
Enter your text: My text My Text
Inputs can be very useful when you want to run a program multiple times with varying inputs as well as when developing a command-line application.
Mini Project: Try to do the same project you did in the last chapter, except with an
input() this time.
num1 = input('Enter your first number: ') num2 = input('Enter your second number: ') res = int(num1) * int(num2) print(str(res))
Chapter 7 - Final Project
Congratulations! You have made it all the way through this tutorial and are ready to attempt the final project!
Instructions: Make a movie theater ticket machine that sets the price for adults at $12 and $5 for children. Make it so you can input the number of adults and children and have it print the total cost. Good Luck!
num_adults = input('Enter the number of adults: ') num_children = input('Enter the number of children: ') adult_cost = int(num_adults) * 12 children_cost = int(num_children) * 5 res = adult_cost + children_cost print('The total cost is $' + str(res))
Chapter 8 - Additional Concepts/Tips
Part 1 - Common Modules
Apart from the basic language itself, Python also hosts a wide variety of modules and libraries such as random or math. I have listed some common ones below with their documentation:
- random: https://docs.python.org/3/library/random.html
- matplotlip: https://matplotlib.org/#
- requests: https://requests.readthedocs.io/en/master/
- math: https://docs.python.org/3/library/math.html
- tkinter: https://docs.python.org/3/library/tk.html
- flask: https://flask.palletsprojects.com/en/1.1.x/
Each of those are useful for different tasks such as making website and GUIs.
Tkinter is one of my favorites
Part 2 - Tips and Tricks
To end this tutorial off I have added some tips and tricks that I have picked up over time:
- Make relevant names for variables: This is one of the most useful things a programmer can do as it keeps their code organized and clean which improves workflow.
- Don't try to learn too much at one: One mistake beginners often make is trying to learn too much at once. This often causes them to feel overwhelmed and remember less of the information.
- Don't over-engineer code: Over-engineering code is a common pitfall that many people experience. To avoid this from happening set a clear goal for a project and stop yourself from continuing once that goal has been reached. This will allow you to find new and more interesting projects rather than to just get stuck on one.
- Learn the basics: Many people often try to immediately delve into complicated and challenging code, foregoing the basics of the language. This creates problems later in your journey so it is best just to stick with the basics at first.
So that's it for this tutorial and please leave any suggestions/questions down in the comment section and I will get back to you. Thank you for reading!
Nice job, but I have some suggestions:
- Add a section for modules
- Add functions that are used a lot
- Add a small section for small details or tips
I hope these help!