Python basic hand-on tutorial on using built-in functions
Python is one of the popular programming languages because of its elegant syntax, dynamic typing and interpreted nature.
If you have not gone through the “Getting Started with Python”, I suggest you read it to get the most of this tutorial.
You can use print() function to display the data to the screen. After starting the IDLE you can enter the print()function to see how it works. Try the following code in the shell and check your output is correct as give below.
>>> print("Hello World")
>>> print("\ "Hello\tWorld"")
Here we used three escape characters that are embedded inside the text. Escape characters interprets differently when place inside the text.
\n escape character
It acts as a line feed and move the text one line down. It is very useful if you do not want to repeat the print and display text on several lines.
\t escape character
It is tab function and separate each words with fixed tab gap.
\” escapes character
Use this to display the text in double quotes as an output.
London Coffee Shops
Now consider the following scenario:
TimeOut London published list of London's best cafés and coffee shops. Some of them included are: The Attendant, Curators Coffee Studio, Department of Coffee & Social Affairs, FreeState Coffee, Look Mum No Hands, Rapha CC, Workshop Coffee Co and Timberyard Seven Dials.
Write the code in shell that display the names of these places in a layout as shown in the sample run below, with heading: List of some “London Coffee Shops”
An example runs of the code:
List of some "London Coffee Shops"
* The Attendant
* Curators Coffee Studio
* Department of Coffee & Social Affairs
* FreeState Coffee
* Look Mum No Hands
* Rapha CC
* Workshop Coffee Co
* Timberyard Seven Dials
Now see the following sample solution. Go through it and make sure you understand the code and the execution output.
Now look at some of the great books turned in to great movies:
Life of PI by Yann Martel, Paperback, £6.29
The Lord of the Rings by J.R.R. Tolkien, Paperback, £18.74
To Kill A Mockingbird by Harper Le, Hardcover, £12.91
The Girl with the Dragon Tattoo by Stieg Larsson, Kindle Edition, £4.99
Lord of the Flies by William Golding, Paperback, £5.99
Write a code that displays the names of these books in following layout with heading: Great Books turned in to Great Films.
An example runs of the code:
Great Books turned in to Great Films
Life of PI
The Lord of the Rings
To Kill A Mockingbird
The Girl with the Dragon Tattoo
Lord of the Flies
Now see the following sample solution. Go through it and make sure you understand the code and the program execution output. Note down how to combine the text with the escape character \n and \t. The print() function will process the ext and prints it in clean format as show in the code below. If you use line feed escape character twice \n\n the text will move two lines down when displayed. Similarly using tab twice or more times \t\t will result in equally increment of space.
In programming we categorise data into their specific types. You can use type() built-in function to get the data type of data. You need to place the data inside the parentheses of type().
Python has following data types that it uses to identify and categorise data.
String for any text
int for all integer numbers
float for all decimal numbers
bool for boolean values
Go the the Python IDLE shell mode and enter the following code.
|data type is str (String) means text.
|data type int (Integer) means whole number.
|data type is float means decimal number.
|data type is bool (Boolean) means boolean values.
|data type is list means collection.
|data type is dict means collection
List and Dictionary are collections. They act as containers to hold groups of data, more on this later.
This shows that Python determines the type of data from the data itself and records it internally and remembers it.
It is short form of length. It can be used to get the size of string/text in Python. You can place the string in the parentheses of the function and it will calculate the size of text by counting each character including the blank spaces. We can use it in many other ways and provide other data beside strings, we will cover it later.
See below the execution of this function in the Python shell.
By now you must be comfortable and indentifying some of the built-in functions in Python and how to use them in an interactive mode by using the Python IDLE shell mode.