Python Lab Manual for Biginners, Study Guides, Projects, Research of Computer science

Starting with Python for the beginners. The document is based on Python 3.7.

Typology: Study Guides, Projects, Research

2019/2020

Uploaded on 03/09/2020

muhammad-ahsan-naeem
muhammad-ahsan-naeem 🇵🇰

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
©Department of Mechatronics and Control Engineering, University of Engineering and
Technology, Lahore, Pakistan
MCT-242 : COMPUTER PROGRAMMING-I
using Python
Prepared By:
Mr. Muhammad Ahsan Naeem
Ms. Qurat ul Ain Masud
pf3
pf4
pf5

Partial preview of the text

Download Python Lab Manual for Biginners and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

©Department of Mechatronics and Control Engineering, University of Engineering and Technology, Lahore, Pakistan

MCT-242 : COMPUTER PROGRAMMING-I

using Python

Prepared By:

Mr. Muhammad Ahsan Naeem

Ms. Qurat ul Ain Masud

The first Program:

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.

Syntax of Python:

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.

Syntax of print() function:

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!" )

  • Repeated String →If you want to print repeated characters; for example, ten dollar signs $ ; of course the first direct approach is to write like: print("$$$$$$$$$$") But Pythons provides a better method to be used inside print function shown here: print("$" * 10)
  • Concatenation →Multiple strings can be concatenated/joined using a + or , sign. Both of these operators have certain advantages over the other. See the output of this line: print('Computer','Programming') You will see single string Computer Programming on the screen. Note that a space is inserted automatically in between two strings being concatenated. The white-space is not inserted using a + sign as shown here: print('Computer'+'Programming') Following line also has the same output as of print('Computer','Programming') print('Computer' , 'Programming') Because the spaces around , sign are ignored by the Python Interpreter as described earlier. Now try this: print('5+5=' , 5+5) You will see this output: 5+5= 10 Again, there is space in between two parts being concatenated. If you don’t want to display space before 10 in above output, the obvious choice is to use + symbol instead of , but if you try this you will get an error: print('5+5=' + 5+5) TypeError: can only concatenate str (not "int") to str

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.

Tasks:

[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.



Using sep and end in string concatenation:

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: