If learning to code with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.
Jump To:
Hello, World!
When learning to code, you must start somewhere. Youāre not going to jump in and already know how to do things. Some things will look like magic at first. As you go along youāll learn more and more about how those magic things actually work.
The first program most programmers start with is called "Hello, World". This program is the starting point because it shows some of the languageās syntax and it feels good when you can see the output of your work. This tradition has been around since 1974 when it was first introduced into a programming tutorial book.
Your First Program!
In whichever editor you choose (eg Python Tutor, Trinket, IDLE, etc), type in the code below and run the code. If you donāt know how to run the code, you can always refer back to the Where Do I Code post.
print("Hello, World!")
Depending on your editor, your output will show a little differently, but it will still be displayed. If you got it to work, congratulations! Youāve built your first program!
Try it again, but this time use your own words. Leave the quotes and put your own words inside them.
Um, It Didnāt Work. Now what do I do?
If your code didnāt work, that is okay. This happens ALL the time. In How Do I Code, we talked about bugs and debugging? You have a bug and you need to debug your program. Here are the steps we want to go through.
- Look at the code you last wrote. Itās likely to be the culprit.
- Make sure the syntax is good.
We have yet to go over the syntax of anything in this program. So, here are some things to check. If you donāt have them, go ahead and fix your code. You can run your code after every change to see if it works.
-
print
should be lowercase - There should be parentheses surrounding your words
- Your words and their punctuation should be surrounded by quotes (like this
"puppy"
)
Some Magic with Print
In How Do I Code, we talked about documentation and the importance of reading it. I know it can be scary, but I promise it will help. Letās take a look at what Python docs have for this print()
. You can go to the docs here: āāhttps://docs.python.org/3/library/functions.html#print
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
āā
This is showing us the syntax of a print statement and what arguments are required and allowed. Remember, arguments are the things that go inside the ()
and are separated by commas. There are five arguments; Weāll go over the first three. Any arguments that have an =
are not required as they had defaults set to whatever is after the =
. Here are the three weāll work with.
-
objects
refers to whatever you want to print and the*
just means you can put as many things, separated by commas, as you want. -
sep=' '
is saying that each of those things, or objects, will be separated by a single space, by default. If you were to givesep=', '
your objects would be separated by a comma and a space. -
end='\n'
says that at the end of whatever you are printing, it is giving a new line. Itās basically hitting the enter key after you print. You can change that if you want by giving something other than\n
for the end. Sometimes it can be helpful to give a separator and/or a different end of the line. Here are a few examples. Try it out for yourself.
# print() separator example
print("cat", "mouse", "dog") # prints cat mouse dog
print("cat", "mouse", "dog", sep=", ") # prints cat, mouse, dog
print("cat", "mouse", "dog", sep="123") # prints cat123mouse123dog
# print() end example
print("1st print", end=", ") # prints 1st print,
print("2nd print")
Do you remember?
Here's some practice challenges. Letās practice what weāve learned so far. Go ahead and comment on this post with your answers. If you don't remember, no worries, you can always go back to read sections again.
Can You Fix What's Wrong with These?
Some of these have more than one right answer. Believe in yourself and give it a try.
Print("Hi there")" |
print"(Bom Dia!, sep="... ")" |
print("āāNĒ hĒoā, sep" - ") |
print("”Hola"!) |
print.("Assalamu Alaikum", end=" ") |
Make some example print statements and give their output
Print Statement | What Would the Output look like | |
---|---|---|
Just words | ||
With a different Separator | ||
With a different End |
Top comments (0)