Lesson 1: Printing
Python is a beast of a programming language, but it doesn't have to be scary. In today's lesson, we'll start at the simplest and easiest starting point we can.
Printing
The very first thing everyone learns when they start coding is how to show some text on the screen. That's called printing.
We can have the computer show stuff to us by using a
print- Type out the word print, followed by two parentheses
- Put whatever you want to print inside the parentheses, in quotations.
You might be wondering what we can print in Python. Let's start by printing some words. Try running the code below:
Congratulations! This is your first Python program!
But what did we actually do, though? We printed the phrase "hello world!" to the output console. The output console is a simple screen that shows you just text (no windows or fancy graphics).
Strings
Notice how Python only prints whatever we put inside the parentheses and without the quotation marks that we added. Why was that?
"hello world!"Print Formatting
Every time we write a new
print()If you don't put anything in the parentheses of a
printUsing empty print statements throughout your code is helpful when you want to space out your text.
Printing Things Next To Each Other
You can print two strings next to each other in one
printThe first way is by using concatenation. Concatenation is a fancy term that just means combining two strings together. All you need to do is to put a
+Concatenation directly combines strings from end to end, so you may need to put a space in one of the strings if you want it to print out like you want. What's the difference in output between these?
An alternative way is to use commas instead. This will put a comma in between the strings for you. What's the difference in output between these?
Rules for Printing Strings
You may have heard of the term "grammar" before. Grammar refers to the rules that someone speaking a language needs to follow so their sentences make sense. For example, "Today I went to the store to buy candy" makes grammatical sense. "Candy went me store" doesn't make any grammatical sense at all, and we really hope you don't talk like that.
In the same sense, programming languages have their own grammar, under a different name, called syntax. Computers are much more particular about following syntax than your English teacher might be about correct grammar. If your code isn't formatted correctly, your computer literally doesn't know what to do. More likely than not, your program will crash (meaning it stops abruptly) and your computer will spit out a
SyntaxErrorWhat does Python's syntax look like? Well, there's a lot of rules, but so far you probably have at least identified a few dos and don'ts.
For example, in Python, a string is identified by any text enclosed in quotations. If you don't enclose your string in a pair of quotation marks, your computer won't run your program You can use either single quotation marks
' '" "Take for example, this scenario: try printing the sentence
'I don't know how to code yet, but I am learning!'When first learning how to code you might encounter some errors when typing out your code. Python likes to be very precise on how it wants code to look before running it. Every detail matters! To make sure that your code is ready to run, follow some of these rules so that you don't get a
SyntaxError- Make sure that the word is all lowercase
print - The parentheses of your print statement should be closed. This means that you need to make sure you have both sides of the parentheses, one at the beginning and one at the end.
(...) - Make sure that your strings have closed quotation marks. Again, make sure you have both sides of the quotation marks.
"..."