Open up the python interpreter on your command line by the following...
$ python3
Type the following one by one into the python interpreter. I'll explain each line one by one later but try to figure out what the result of each line is before typing it out and hitting enter.
print("A string")
print('This is a string too')
print("stuff" + "and more stuff")
print("The char:", 'a')
print("The string: ", "stuff")
print("The digit", 5)
print("The float ", 4.0)
print("Printing multiple stuff:", 'a', "stuff", 5, 4.0))
print("First Line \nSecond Line")
print("\tI am a tabbed Line.")
# print("I am a comment so I won't print. :(")
print("""
This is a bit weird.
But acts as if I added newlines.
""")
The explanations are below make sure you ran all of them before checking the explanations.python3 ex1.py
Explanations:
Line 1:
print("A string")
Using the print function you display "A string" which is a string to the console. You have to include the quotations otherwise it'll be treated as a variable which you'll learn later.print('This is a string too')
This is also a string but using single quotes you can use either whatever you prefer. print("stuff" + "and more stuff")
What you are doing here is called concatenation which is a fancy word for joining strings together. You are doing this by using the + symbol adding the two strings to form one string and displaying it onto the console.print("The char", 'a')
Using the , symbol is the format symbol in python which allows you to insert the character 'a' into the string. Also notice that is followed by the leter 'c' indicating that it is expecting a character.print("The string", "stuff")
This time you are using the string formator symbol but expecting a string.print("The digit", 5)
Formating a digit instead of a string or character.print("The float", 4.0)
Formatting a float. A float having digits after the decimal place.print("Printing multiple stuff:", 'a', "stuff", 5, 4.0))
Now you are using multiple commas to print out a character, string, integer and a float.print("First Line \nSecond Line")
The "\n" is an escape character and creates a new line thus showing you the string on two different lines on the console. There are many escape characters and if you want to see all of them you can see them in the python documentation.print("\tI am a tabbed Line.")
\t creates a tab and displays it onto the console.# print("I am a comment so I won't print. :(")
This is a comment so it won't show up. This is how you write notes in your program without it being interpreted as code.print("""
This is a bit weird.
But acts as if I added newlines.
""")
Displays on two lines without the use of a \n escape character. Kinda weird but works.exit()