Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Tutorial - 2
stoporinjail (116)

Hello everyone! Welcome back to another Python tutorial.

What we will be learning:

Lesson 2

  • Lists
  • Imports: Random, time (basic stuff)
  • Dictionaries
  • Loops
  • If, Elif, else statements
  • User inputs
  • Operators

Lesson

Without further ado, let's get started!

Lists

Variables overwrite each other. If you create one variable and then change it to another value, the second one overwrites it.
For example:

name = "Albus"
name = "Severus"
print(name)

If we run that, it responds with: Severus.
This is because, on line 1, you put Albus in the box. And then on line 2, Albus goes out of the box and Severus goes inside. When you print it, it just responds with Severus because that's the most recent one.

So yes, there is a way to put more than 1 thing into the box. So you generate a list.

How to:

list_name = ["value1","value2","value3"]

So printing them out is exactly like a regular variable,

print(list_name)

It prints out the whole list, but you might notice that all of it is in square brackets and quotes.
To remove it, you need to convert it.

new_list = str(your_existing_list)[1:-1]

Now, if you print the new_list out, you will get no brackets.
Now, of course, there is a way to print out a mere part of the list.

print(your_existing_list[0])

The number you put depends. If it is the first item on the list, you put 0. Second gets 1, and so on. You can also use negative numbers, so the last gets -1, second last gets -2, and so on.
Reference:
1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
"v","v","v","v","v","v"

Example:

characters = ["harry potter","Hermione Granger","ron weasley"]
c = str(characters)[1:-1]

print(f"My favorite characters in Harry Potter are {c}")

Imports

Imports, aka modules are special things you can use to do special things. A lot of them are useful, such as time, random, and sys, as I am going to tell you today.

Time

Let's start with time. The most used one of time is sleep.

To first use the module, you must import it.

import time

Once we have it imported, we can now use it.

time.sleep(seconds)

The time.sleep() will pause the program for a certain amount of seconds. Yes, you can use decimals.

Example:

print("Hello, welcome!")
time.sleep(1)
print("Here are the instructions...")

If you run it, you will get Hello, welcome! followed by a short pause, then Here are the instructions...

Random

You will be learning about 2 random features today, randint, and choice.

First, import the module:

import random

Randint makes a random number between a range of your choice.
example:

print(random.randint(1,200))

Response will be a random number between 1 and 200.

random.choice

Choice will pick a random item in a list.

choices = ["Yes","no","maybe"]
print(random.choice(choices))

Remember, you need to close both paretheses because we opened 2. Output: random choice.

Loops

Loops well, loop the program for a certain amount of time.
Number loops loop it for a specific amount.

for this_can_be_anything in range(1,10):
    print(this_can_be_anything)

the this-can-be_anything will be equal to the amount it has looped. for example, if it repeated for the 2nd time, this_can_be_anything would be equal to 2.

List loops:

list = ['1','2','3']
for number in list:
    print(number)

Number will be equal to the next value of the list.

User input

User input allows you to ask a user a question, and then that user will respond.

variable_name = input("Your question")

It will store the user's answer into the variable.
Example:

name = input("What is your name")
print(f"Hi, {name}.")

You will enter your name, and it will say Hi, [yourname]

If, elif, else

THis is useful for user input.

How to:

if variable == 'value':
   print('something')
elif variable == 'value2':
    print('something')
else:
    print('something')
The variable can only equal either if or elif, or the else. You can do multiple ifs, but elif means it has to be either elif, if, or else.

```py
name = input("What is your name")
if name == 'Not a name':
    print('Are you sure?')
elif name == 'Voldemort':
    print("uh oh")
else:
    print("Hi!")

If i run the program and enter "sg", I will get Hi

Operators

Operators are like equations in Python. You make them, and then they respond with the answer.
Example:

print(5+10)

It will respond with 15

Dictionaries

Remember when we did lists? Well, we're going to expand that. You can do

list = ['eyecolor','haircolor','nationality',]

etc
But we don't know what "brown" or "black" means.

How to:

dictionaryName = {
"thing1":"value"
"thing2":"value"
}
print(dictionaryName)

This prints out the whole thing^^
To print a certain one, do:

print(dictionaryName["thing"])

Conclusion

This is the end of Python Tutorials. Bye bye!

Comments
hotnewtop
Danffy (0)

Thanks, this was very helpful. After these 2 lessons i learned how to make a working calculator ;]

AliceMorgan (0)

I am a student of Information technology and I don't know any of the programming languages. I tried to study but it all went in vain. Then one of my colleagues of Kuwait best CIPD writing help told me about the replit and i started taking your python lessons. they all are amazing, I am now understanding python language and I can now do basic level programming in python too.