DEV Community

Cover image for Your First Line of Code
Vicki Langer
Vicki Langer

Posted on • Edited on

Your First Line of Code

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!")
Enter fullscreen mode Exit fullscreen mode

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.

  1. Look at the code you last wrote. It’s likely to be the culprit.
  2. 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)
Enter fullscreen mode Exit fullscreen mode


​​
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.

  1. objects refers to whatever you want to print and the * just means you can put as many things, separated by commas, as you want.
  2. sep=' ' is saying that each of those things, or objects, will be separated by a single space, by default. If you were to give sep=', ' your objects would be separated by , a comma and a space.
  3. 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")
Enter fullscreen mode Exit fullscreen mode

Welcome to the world of programming. Now that you’ve done a couple lines of code, you are a programmer. As a programmer, we are always learning. So, try practicing with these challenges below. Then, you’re welcome to keep going and click the next post at the bottom.


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 (4)

Collapse
 
mrokdev profile image
Paul

maybe the code should like this:
print("Hi there")
print("Bom Dia!", sep="... ")
print("Nǐ hǎo", sep=" - ")
print("¡Hola!")
print("Assalamu Alaikum", end=" ")

Collapse
 
vickilanger profile image
Vicki Langer

That looks great Paul! Are you just starting to learn how to code?

Collapse
 
mrokdev profile image
Paul

Yes, I want to learn Python basics. It's very popular and useful programming language and I want to check if programming is for me.

Thread Thread
 
vickilanger profile image
Vicki Langer

That's awesome! Let me know if you have any questions.