Python Tutorial - 2
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
The operator for multiplication is *
The operator for division is /
Subtraction is -
Addition is +
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 Tutorial 2. Bye bye!
Lesson 3
Lesson 3 will be released soon, probably 12/5/21. May be adjusted.
ty
From Nigeria and i want to say God bless you sir
🌍...|Hello world !|Thank you for this tuto.
College students will research with observe gear that adapt to the ONLINE TUITION WEBSITE IN PAKISTAN abilties of a pupil. This means above common students shall be challenged with harder duties and questions when a certain degree is executed. Students who enjoy difficulties with a topic gets the opportunity to exercise greater until they attain the desired degree.
although there should be more
very helpful, never coded in python before
when's lesson number 3?
This is a very simple tutorial same as the first lesson. I think it is great for beginners who have never done programming before. Even tho I think it should have other simple stuff rather than imports at the beginning.
feel regret I did not know this before I read your article, thank for sharing with us! https://theimpossiblequiz.online
cool this is a really good tutorial
Well, this got me thinking what other workouts are good for those of us who find ourselves on the road or have limited equipment options. Business Savvy
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. Business World Intelligence
This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post! Montgomery Archives
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. Fly Safely
If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state. Computer Medics of Central Winconsin
I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. Ultra Internet Income
Hi to everybody, here everyone is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily ISB Computer
I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place West Palm Beach Business List
I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog! Maryland Internet Marketing
I am looking for and I love to post a comment that "The content of your post is awesome" Great work! Will Write 4 Food
They're produced by the very best degree developers who will be distinguished for your polo dress creating. You'll find polo Ron Lauren inside exclusive array which include particular classes for men, women. Loughborough Innovation
This is a splendid website! I"m extremely content with the remarks!. WI Business
I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.. United 4 Safety
I think it could be more general if you get a football sports activity Thick Business Band
Thanks, this was very helpful. After these 2 lessons i learned how to make a working calculator ;]
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.
number 3?
Lesson #3 ?