Python Print Function
Every programming language has a command that will output text on a computer screen. In Python, it’s called the print function. Most electronic devices communicate in some way with the user. Usually, they display some sort of text. Regardless of the coding language, mastering text (also called a string) output is usually the first step in learning to code.
Printing as a Function?
When you think about it, you’re “printing” onto a screen.
This image demonstrates the output of 2 print functions: the first line outputs the words “Hello world!” and the second line does the arithmetic and outputs the result of 12.
Python’s print function has a fair amount of syntax, just like Java and C. Brackets are needed around anything you want Python to output.
Let’s look at another example:
These lines would output the math statement on one line and the result on the next. That’s okay, but we’re not used to seeing it that way.
So, a coder’s next task is to learn how to manipulate the output to make it more user-friendly. And, of course, Python’s print function can do that when you learn more syntax!
Python Print Function Syntax
Syntax has the same meaning in coding as it does in English. In coding, syntax defines the rules for a specific function. If a programmer doesn't follow the rules, errors will occur. Share on XThat’s more like it! All it took was learning some new syntax to put the two statements together:
The comma between the string “10 + 2 = ” and math statement 10 + 2 tells Python you want to join two elements using the same print function. Now they are both on the same line, and it looks right.
So, how does print work?
The most important part of coding is understanding how and why things work the way they do. Kids need to be asked the right questions to help them understand how a function works. Share on XLearning a bit more about Python’s print function can make a big difference!
Syntax Errors?
Python’s print function is pretty straight forward, and although it does have a lot of syntax at the advanced level it can still be taught to kids. Kids will always learn something new when they’re ready for it.
It's pretty easy to make a syntax error until you figure out the rules. But, it's also the best way to learn how to code. Share on XPython won’t run the program if it encounters a syntax error. It’s just like using a spell-checker to correct your English.
Designing Output
Coders can create interesting designs with Python’s print function.
It’s always a good idea to plan your program on paper first. Grid paper is an excellent tool to have kids work out their designs.
If you’re interested in teaching Python I do have a resource to start you off. It includes answer keys as well as lessons with teacher notes. Click here.
You can also buy the full unit (which includes a test) here.
Graphics with Python
Maybe you’re more artistic? Check out Python Turtle’s Basic Graphics Functions.