Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 21, 2021 05:24 pm GMT

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. Youre not going to jump in and already know how to do things. Some things will look like magic at first. As you go along youll 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 languages 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 dont 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! Youve 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 Didnt Work. Now what do I do?

If your code didnt 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. Its 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 dont 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. Lets 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='
', 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; Well 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 well 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='
    '
    says that at the end of whatever you are printing, it is giving a new line. Its basically hitting the enter key after you print. You can change that if you want by giving something other than
    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 exampleprint("cat", "mouse", "dog")  # prints cat mouse dogprint("cat", "mouse", "dog", sep=", ")  # prints cat, mouse, dogprint("cat", "mouse", "dog", sep="123")  # prints cat123mouse123dog# print() end exampleprint("1st print", end=", ")  # prints 1st print,print("2nd print")

Do you remember?

Here's some practice challenges. Lets practice what weve 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 ho, sep" - ")
print("Hola"!)
print.("Assalamu Alaikum", end=" ")

Make some example print statements and give their output

Print StatementWhat Would the Output look like
Just words
With a different Separator
With a different End

Original Link: https://dev.to/vickilanger/your-first-line-of-code-5ebi

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To