Basic Python Tutorial
aguy11 (128)

Notes
Python uses # to display single-line comments
Do NOT place semicolons at the ends of statements

Variables in Python

What is a variable?
A variable is a small piece of data inside your program. To explain this a bit better I am going to explain it as if it were to be a box. Imagine this box with a small piece of paper on it that says 'color'. Inside this box, we put another piece of paper that says 'green'. Therefore, this variable is named 'color' and contains a bit of text that states 'green'.

Types of variables in Python
There are many types of variables in Python. So many that I could go on forever listing them. Here are a few types:

  • String: This type of variable is a piece of text
  • Int: (or integer) Is a number
  • Float: A decimal
  • Boolean: A variable containing either True or False.

Declaring a variable in Python
To declare a variable we simply have to type its name followed by an = sign, and then what the variable holds. Note: Always end and start strings with " or '. Here are some variables declared in Python:

eye_color = "blue"
hair_color = "brown"
true = True
false = False
age = 10
age_by_five = 50
pounds = 70.56
temperature = 70.59

Note: operations to floats and integers are usual: +, -, *, /.

User input in Python

User input is what the name states it is. It is (usually) a variable that contains something that the user typed. Here is a way to get user input:

choice = input("You can type some input questions here!")

As you can see, you can declare an input variable by using the built-in input() function followed by a string. But what if you try this:

number = input("Pick a number")
num2 = number * 2

Num2 is not going to be number multiplied by 2. It will be number and number meshed together. This is because the input is originally branded a string, and you must declare it an integer for it to be one. So here is the right code snippet:

number = int(input("Pick a number"))

Yes, you would use int() to declare anything an integer.

Displaying messages on the screen

Of course, in Python, there must be a way to tell the user something. This is done using a print() statement:

print("You can print stuff in here!")

But what if you wanted to display something and then a variable? I will show you two ways to do this:

test_var = 34
print("The test variable is " + test_var)

That is the first way, though it is highly inefficient. The plus symbolizes that you are adding some more words/numbers to the string. Here is another, and more efficient way:

test_var = 34
print(f"The test variable is {test_var}")

The f at the beginning of the print() stands for formatted. This, as now revealed is called a formatted string or an f string. As you might also capture, the variable that you want to add has to be enclosed in squiggly brackets, or otherwise, this method wouldn't work.

If/elif/else statements in Python

No language goes without having a way to detect if something is true or not! This goes for python too. There are three words that you would use to symbolize this: if, elif, and else. If, as you probably understand presents us with a way to check if something is true. There must be one of these in order for the if sequence to work. Elif stands for else if and is only checked through if an if statement above is not true. There can be any amount of these. Lastly else. This is only run through if all of the above if/elif statements are false. Then it is automatically activated. There can be one
or none of these statements.

Declaring an if/elif/else statement
This is simple. If the statement is an if or an elif than you type that, then the condition you are checking for and then a colon. Otherwise, just type else and then a colon. It should look something like this:

gender = input("Are you a) male or b) female?")
if gender == "a":
   print("You are male!")
elif gender == "b":
   print("You are female")
else:
   print("That is strange...")

And yes, of course, you must indent the process that you want the program to run if the conditions are true.

Note: The process I am using here is ==. This means the same thing as =. There are many other kinds like:

  • <: Less than
  • bigger than symbol: Bigger than
  • <=: Less than or equals to
  • bigger than and =: Bigger than or equals to
  • !=: Does not equal to

There are also additions you could add if you wanted it to check for multiple processes like and and or. Usage of these would look something like this:

layers = 10
if layers < 15 and layers > 5:
   print("You have an acceptable amount of layers")
elif layers == 3 or layers == 4:
   print("You have a mildly acceptable amount of layers")
else:
   print("Your amount of layers is unacceptable!")

Lists in Python:

Okay, so you know what a variable is. But how do you make a collection of variables? This is quite easy. In Python, it is called a list and is created by using square brackets and putting the information inside. For example:

soup_items = ["chicken", "noodles", "cheese", "corn"]
print(soup_items)

Lists can contain any type of variable. But if you were to run this program you would see the following: ['chicken', 'noodles', 'cheese', 'corn']. This would be unusual for the user because they would not expect to see the square brackets, nor the quotes around the words. So how would you solve this?

Python List Iterating and Indexing
Python's lists use indexes that are quite usual. Index 0 would be the first variable. Index 1 would be the second and so on. There are also backward indexes such as -1, which is the last variable, -2 which is the second to last variable, and so on. And how would you use these indexes? The answer is, again, simple. you would first type the name of the list combined with square brackets that contain your index. Somewhat like this:

soup_items = ["chicken", "noodles", "cheese", "corn"]
print(f"At index 0 we have {soup_items[0]}")
print(f"At index -3 we have {soup_items[-3]}")
print("At index -2 we have {soup_items[-2]}")
print("And at index 3 we have {soup_items[3]}")

Changing lists
There are a few ways to change a list. Here are some:

nums = [1, 2, 3]
nums.append(4)
nums.insert(0, -1)
del nums[1]
print(nums)

The first method, .apppend() simply adds the item listed in the parentheses to the list listed before the dot as the last item. The second method .insert() uses two arguments: 1) The index position and 2) What to add. The first argument is typed first and then the second. The last method, del deletes an item from a list by index position.

Loops in Python

There are two kinds of loops in the Python language. A while loop and a for loop.

While Loops
While loops are to be run when the condition that is stated after is true. The condition is declared exactly like in an if statement. Let's look at an example:

number = 0
while number > 10:
  print(f"The number is {number}")

And again, you must indent the code inside the loop.
Note: while True will create an infinite loop.

For Loops
For loops are a bit different from while loops. They are used to run through a list or just to repeat something a given amount of times. I will show you an example and then explain further:

soup_items = ["chicken", "noodles", "cheese", "corn"]
for soup in soup_items:
   print(f"{soup} is in your soup!")
for add in range(0, 3)
  soup_items.append("water")

First of all, let me explain the temporary variables. They are given after the 'for' and can only be used within that loop. In for loops that sort through lists these are the items in the lists and in the range() loops they are numbers in the range. Now, let me explain the for loops that sort through lists: The variable, as listed above is an item in the list. The last thing that comes in the loop is the list you want it to sort through. So the structure would be something like this: for (temporary variable) in (list). The last type of loop is the range() for loop which repeats the process inside the loop as long as the variable is inside the range. No worries, the variable increases by one each time the program finishes the loop. The range() function takes two or one number. If you put in one number, the program will repeat the contents of the loop that number of times. If you put in 2 numbers that means the program will start with your temporary variable set to the first number and will end when it hits the last number. Lastly, the loops must end in a colon and the contents must be indented.

Hopefully, I explained everything properly and you now understand the basics of Python. Please let me know if I missed something or should try to explain something better.

You are viewing a single comment. View All
dillonjoshua68 (73)

Oh ok, what was the first language u learnt? @johnstev111