fbpx
1- Introduction
2- Python Basic
3- Learning Python
4- Python Literals
5- Arithmetic operators
6- Function INPUT() and STRING operations
7- Comparison Operators and Conditions
8- Loops in Python
9- Logic and bit operators
10- Lists and Arrays in Python
11- Functions
12- Tuples, Dictionaries
13- Conclusion
14- Practice Exams

L3.1 Hello World Program and Print function

Its time to write your first program ‘Hello World’.

In the editor windows, type in print(“Hello, World!”) and run it. You will see results as Hello, World.

Note: The first program consists of the following parts:

  • the word print;
  • an opening parenthesis;
  • a quotation mark;
  • a line of text: Hello, World!;
  • another quotation mark;
  • a closing parenthesis.

Print function with backslash \ and new line character n

Let’s enhance the previous code with a long string.

print(“The itsy bitsy spider climbed up the waterspout.”)
print()
print(“Down came the rain washed the spider out.”)

Lets add a backslash \ and a character n (new line character) to see what happens

print(“The itsy bitsy spider\nclimbed up the waterspout.”)
print()
print(“Down came the rain\nand washed the spider out.”)

Can you see the difference in the output?

Conclusion: In Python strings the backslash (\) is a special character which announces that the next character has a different meaning, e.g., \n (the newline character) starts a new output line.

The print() function – using multiple arguments

Now, write this code and notice the output

print(“The itsy bitsy spider” , “climbed up” , “the waterspout.”)

The itsy bitsy spider climbed up the waterspout.

There is one print() function invocation, but it contains three arguments. All of them are strings. The arguments are separated by commas. In this case, the commas separating the arguments play a completely different role than the comma inside the string. The former is a part of Python’s syntax, the latter is intended to be shown in the console.

Conclusion: a print() function invoked with more than one argument outputs them all on one line; the print() function puts a space between the outputted arguments on its own initiative.

The print() function – the positional way of passing the arguments

Write this code and notice the output

print(“My name is”, “Python.”)
print(“Monty Python.”)

The way in which we are passing the arguments into the print() function is the most common in Python, and is called the positional way (this name comes from the fact that the meaning of the argument is dictated by its position, e.g., the second argument will be outputted after the first, not the other way round).

The print() function – the keyword arguments

Write below code and see the difference

print(“My name is”, “Python.”, end=” “)
print(“Monty Python.”)

Keyword arguments are the ones whose meaning is not dictated by their location, but by a special word (keyword) used to identify them. There are two commonly used keyword arguments end and sep. We have used one of the keyword end  in the above example.

Note following for keywords

  • a keyword argument consists of three elements: a keyword identifying the argument (end here); an equal sign (=); and a value assigned to that argument;
  • any keyword arguments have to be put after the last positional argument (this is very important)

Now, write below code and see the difference from previous code.

print(“My name is”, “Python.”, end=”*”)
print(“Monty Python.”)

We hope you can realize that you can assign any value to end argument after equal sign , in this case, a star *

Now, we will try another keyword argument , sep. Write below code and notice the output

print(“My”, “name”, “is”, “Monty”, “Python.”, sep=”-“)

You should see an output like this

My-name-is-Monty-Python.

Replace the above code with following and see the difference

print(“My”, “name”, “is”, “Monty”, “Python.”, sep=”*”)

How about combining both sep and end….

print(“My”, “name”, “is”, sep=”_”, end=””)
print(“Monty”, “Python.”, sep=””, end=”*\n”)

You should see following output

My_name_is*Monty*Python.*

Important notes for exam

1. The print() function is a built-in function. It prints/outputs a specified message to the screen/console window.

2. Built-in functions, contrary to user-defined functions, are always available and don’t have to be imported. Python 3.8 comes with 69 built-in functions. You can find their full list provided in alphabetical order in the Python Standard Library.

3. To call a function (this process is known as function invocation or function call), you need to use the function name followed by parentheses. You can pass arguments into a function by placing them inside the parentheses. You must separate arguments with a comma, e.g., print("Hello,", "world!"). An “empty” print() function outputs an empty line to the screen.

4. Python strings are delimited with quotes, e.g., "I am a string" (double quotes), or 'I am a string, too' (single quotes).

5. Computer programs are collections of instructions. An instruction is a command to perform a specific task when executed, e.g., to print a certain message to the screen.

6. In Python strings the backslash (\) is a special character which announces that the next character has a different meaning, e.g., \n (the newline character) starts a new output line.

7. Positional arguments are the ones whose meaning is dictated by their position, e.g., the second argument is outputted after the first, the third is outputted after the second, etc.

8. Keyword arguments are the ones whose meaning is not dictated by their location, but by a special word (keyword) used to identify them.

9. The end and sep parameters can be used for formatting the output of the print() function. The sep parameter specifies the separator between the outputted arguments (e.g., print("H", "E", "L", "L", "O", sep="-"), whereas the end parameter specifies what to print at the end of the print statement.

11
0
Wanna ask a question or say something? Go aheadx
()
x
Scroll to Top