Input & Output
›Input and Output in Python
The first step in our programming journey is input and output in Python. These are super important skills that will help you create interactive programs.
Output: Talking to the User
When we want our program to show something to the user, we use
the print() function. It outputs the value in the brackets on the screen.
print("Hello, I'm a Python program!")
Variables: Remembering Things
Variables are like boxes where we can store data. We can give them names and put different types of data inside them. The names cannot have spaces in them.
favourite_colour = "blue"
The value in the variable can be concatenated (joined together) with other strings using either a , or a + .
favourite_colour = "blue"
print("My favourite colour is ", favourite_colour)
print("My favourite colour is " + favourite_colour)
Input: Listening to the User
The input() function lets us ask the user for
information. It's like giving the user a chance to speak to our
program!
name = input("What's your name? ")
Comments: Notes for Humans
Comments are messages in your code that Python ignores. They are written only for humans, not the computer. We use them to explain what the code does or leave reminders for ourselves.
# This is a comment
print("Hello, world!") # This comment comes after code
In Python, a comment starts with a # symbol. Everything after the # is ignored by Python.
We spend more time reading code than writing code. Clear comments help us understand what we did. They're especially useful to explain new ideas.
Putting It All Together
We can use input, variables, and output together to make our programs interactive. Here's a good example:
nickname = input("What is your nickname? ")
print("Nice to meet you ", nickname)
print("Nice to meet you " + nickname)
In this example, we
- Ask the user to input a nickname,
- Store it in a variable,
- Output the variable concatenated with a string in a message.
Now it's your turn to try! Can you make a program that asks for someone's favourite animal and then says something nice about that animal? Press inspire me for more ideas of what to code.
Write a program to...