Beginner Python tutorial – 01 – Print, Variables & Input

Video version:

As the first video in the Python series, this is your first approach to Python but don’t worry, it’s going to be easy. We will learn about:

Print

Print function outputs information to the user. Let’s see an example:

 print('This is the print function')

As you see, you just type ‘print()’ and inside the parenthesis the text surrounded by simple or double quotes.

Variables

Think about variables as a boxes where we store the information. This is useful because we avoid writing the same text over and over (Very prone to mistakes). For example:

name = 'David'
print(name)

The output of this code is “David”. We store “David” in a variable called ‘name’, and now to use that variable, we just use it in prints. When we call variables we don’t need to use quotes, so mind that.

Can we mix text and variables? Of course, you just need to separate them with the plus symbol:

print("Hello, I am " + name + " and I wish you a happy day!")

The Python naming convection is to use all lowercase names with underscores as spaces. For example: first_name or user_age

(Disclaimer: There are more and better ways to use text and variables together but this one is simpler. We will learn more about it in next lessons)

Input

Right now the information flux is pretty one-way. Can we ask the user for inputs? Of course! With the input function. It works pretty much as the ‘print’ function but we need to store the answer in a variable. Let’s see:

user_name = input('Hello. What it is your name?')
print('Hello ' + user_name)

Easy. Right?

We have learnt how to print information, how to ask for user’s inputs and we store them to use in our code.

Well, I’ve taught you a few things. Now its time for you to use them!

Challenge: Madlibs

Have you ever played madlibs as kid? One of you would ask a few questions to the other (“Tell me a noun…now and adverb, now an animal”) and the kid asking would replace the other kids questions in the empty spaces of a text. The results are hilarious

Resultat d'imatges de madlibs

Now it’s time to create your own madlib! You know how to ask the user for adjectives, nouns, colours and whatever you want. After the user has asnwered a few questions, print a text with the answers.

Don’t scroll down. Stop reading right now (Well, after this paragraph đŸ™‚ ) and try to do it on your own. Of course, re-read the first part of this post if you need help. Now, try it!

Did you were successful?

This is my version:

colour = input('Give me a colour: ')
superlative = input('Give me a superlative (largest, kindest...): ')
adjective_1 = input('Give me an adjective (happy, crazy...): ')
body_parts = input('Give me a body part (plural): ')
body_part = input('Give me body part: ')
noun = input('Give me noun (house, trampoline....): ')
animal = input('Give me an animal (plural): ')
adjective_2 = input('Give me an adjective (happy, crazy...): ')
adjective_3 = input('Give me another adjective (happy, crazy...): ')
adjective_4 = input('Give me the last adjective (happy, crazy...): ')

print(f'The {colour} Dragon is the {superlative} Dragon of all. It has {adjective_1} {body_parts}, and a {body_part} shaped like a {noun}. It loves to eat {animal}, although it will feast on nearly anything. It is {adjective_2} and {adjective_3}. You must be {adjective_4} around it, or you may end up as it`s meal!')

Now you can teach your friends how to do madlibs and you can challenge each other to do an even funnier version.

I hope you had fun, I will see you on my next lesson.

Link to the code: https://repl.it/@DavidMM1707/01-Madlibs

Video version: https://www.youtube.com/watch?v=-zK09YNeoRQ

Follow me on Twitter: https://twitter.com/DavidMM1707

My GitHub: https://github.com/david1707

Learn more about:
– Print: https://www.programiz.com/python-programming/methods/built-in/print
– Variables: https://www.programiz.com/python-programming/variables-datatypes
– Input: https://www.programiz.com/python-programming/methods/built-in/input

Second lesson: 02 – Conditional Statements