



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Starting with Python for the beginners. The document is based on Python 3.7.
Typology: Study Guides, Projects, Research
1 / 6
This page cannot be seen from the preview
Don't miss anything!




©Department of Mechatronics and Control Engineering, University of Engineering and Technology, Lahore, Pakistan
Displaying a message on the output screen: To display a message on the output screen we use print() statement in Python. Here is the program to display Hello World on the screen. print("Hello World!") If you run the code, you will see the message displayed.
Every computer language has its own set of rules to type in the statements/functions known as the syntax of that language very much like the grammar we have in human language e.g. rules of punctuations in English.
Let’s observe the above statement in detail: The function to print a message on the screen is print() which is a built-in function. Here the function name is print followed by brackets () inside which we have to write the message within double quotes " that we want to print. To type in Python programs, you have to consider these: Case: Case matters. To Python, print, Print, and PRINT are all different things. For now, stick with lowercase as most Python statements are in lowercase. Try to run the following code: Print("Hello World!") And you will see that it will generate an error something like: NameError: name 'Print' is not defined White Space or Tabs: Spaces matter at the start of the line for proper indentation; something we will see in detail in coming lab sessions. But other than that, you can add spaces. Tab and Space work same for indentation in Python. We will be using Tabs instead of Space for the indentation but the choice is totally up to you. Following lines gives the same output (see carefully the different spaces/tabs): print("Hello World!") print ("Hello World!") print( "Hello World!") print ("Hello World!" )
May be its difficult to understand the error statement at the moment but one simple reason is that the interpreter cannot differentiate whether to use + symbol for mathematical addition or the concatenation. We will explore the solution in next section.
[1] Display the following box (20 asterisks in one row) on the screen using the feature of repeated string display.
[2] Display the following triangle again using the feature of repeated string display:
**
[3] Display the following rectangle, again using the feature of repeated string display. For the spaces too you have to use the repeated feature. In second and third line of the box you have to use string concatenation using + symbol.
As we have seen that concatenating a string using , operator inserts a space in between strings being concatenated. We can control the separator using sep parameter of the print() function as shown here: print('A','B','C','D',sep='--') Here the sep property specifies that the separator between strings will be -- instead of a white space. The output is shown here: A--B--C--D We can also use empty single or double quotes in sep parameter to specify that there will not be any separator between the strings as shown: print('A','B','C','D',sep="") The output is: