View Single Post
Old 2019-11-10, 02:48   #7
Dylan14
 
Dylan14's Avatar
 
"Dylan"
Mar 2017

2×293 Posts
Default The Print Command and your first program

Now in the previous post in this guide I mentioned the "Hello World" program being the first program that aspiring programmers type out in the language of their choice. With our background that we have developed so far, we are now ready to get to this concept of printing messages to the screen, which is ultimately what the "Hello World" program demonstrates.

The "print" command:

Print is a simple built-in function that takes in a string and then prints it to the screen. This is invoked by typing the following:

Code:
print(string)
This is the simplest invocation of the print command. Note here that the "string" can be a chain of strings strung together, using concatenation. We can also format the string using the format command. These will be the subjects of the next two sections of the guide.
Actually, print has some keyword arguments, although these are hardly used. The full syntax for the print command is

Code:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here's what all the arguments mean:
* *objects - specifies what objects should be printed. The * indicates that we can link multiple objects. Objects to be printed have to be separated with commas.
* sep = ' ' - objects are to separated with this string. By default, it is a space.
* end = '\n' - The object to be printed at the end of the objects. By default, it is a new line (note the escape character).
*file = sys.stdout - Specifies where the output will be sent to. The output location must be an object with the write(string) method. By default, it is sent to sysout, i.e., it gets output to the screen.
*flush - Takes either the value of True or False. If True, the stream is forcibly flushed. If False, it is not. The default is False.

Your first program:

Now, we have a function that we can play around with. Therefore, we can create programs with it. The simplest one that we can write is the "Hello World" program, which I have mentioned a few times already. To create this, open up a Python shell/notebook and type the following:

Code:
print("Hello world!")
This will yield the following:

Code:
Hello world!
Congrats! You have now written your first program in Python!

Ok, celebrations aside, this is great, but it is limited to whatever we put in the string. To improve this, we can use the input() (*) command, which will ask for user input. Try this code:

Code:
string = input("Type something: ")
print(string)
This yields, after user input:

Code:
Type something: Welcome to mersenneforum's python guide 
Welcome to mersenneforum's python guide
Now, we can type whatever we'd like to print. Which is always nice.
In the future we can use format() and string concatenation to make more intricate things to print.

(*) The input command will be covered in more detail later.

Last fiddled with by Dylan14 on 2019-11-12 at 23:06 Reason: initial draft
Dylan14 is offline