Friday, November 10, 2017

DATA TYPE PART 1


DATA TYPE PART 1

Data type is the classification of data based on what we can do with them in our program and based on this data can be classified into various categories. Lets take a look at some of this classifications below and also some examples.

Strings data types

The first data type we will consider is the string. A string is a series of characters. In python, anything within a quote is considered a string. The quote could be a single quote (‘’) or double quote (“”). Example of string are:
‘This is a string sample’

“This is another string sample”

Because python allows the use of both double quote and single quotes, there is flexibility of quoting a word within a string just like the following:

‘My friend said to me, “why not learn python!” ’

“What is ‘python?’ ”

In the first string, we quote out “why not learn python!” and in the output of the program, why not learn python! Would be quoted. The same thing applies to “python” in the second string, “python” would be singly quoted in our program output.

There are so many things we can do with strings in python. Let examine some of the things we can do with python strings.

Changing letter case in a string with methods

One of the things we can with strings in python is to change the cases of the words in a string. Let open our text editor and create a new file. name the file “my_name.py

name = “ibrahim temitope”

print (name.title())

save the program and run it. The output of the program would be

Ibrahim Temitope

In the above example, we create a variable called name and store the value “ibrahim temitope” in it. The method title() appears after the variable name in the print() statement. A method is an action that python can perform on a piece of data. The dot(.) after name in the name.title() tells python to make the title() method act on the variable name.

In python, every method is always followed by a set of parenthesis. This is so because methods often requires additional information to do their work. The information required will be provided within the parenthesis. The title() method does not need an additional information, so the parenthesis is empty. The title() converts each word in the string to title case where each word begins a capital letter. This will be useful if for example we want our program to recognize ibrahim, Ibrahim, and IBRAHIM are the same and display all of them as Ibrahim.

Other methods available for formatting strings are:

name = “Ibrahim Temitope”

print(name.upper())

print(name.lower())

The output of the code after running will be:

IBRAHIM TEMITOPE

ibrahim temitope

We may obtain user inputs in any form as user may decide to input data in various style, but we can always format any string to suits our program usage.

Combining strings together

In some cases, we may want to combine two different strings together. Say for example, we want to store a user first name and last name in separate variables, and then combine them to give out the person full name.

first_name = “ibrahim”

last_name = “temitope”

full_name = first_name + “ ” + last_name

print (full_name)

A combination of two or more string to produce a single variable is achieved using the plus (+) symbol i.e. python uses the plus symbol (+) to combine strings. In the example, full_name is created by combining the first and last name with a space between them. This method can be used to compose a complete message using the information we’ve stored in a variable.

first_name = “ibrahim”

last_name = “temitope”

full_name = first_name + “ ” + last_name

print (“Hello ” + full_name.title() + “!”)

In the above example, the full name was concatenated with the gretting message Hello. If the code is run, the output of the code will be:

Hello Ibrahim Temitope!

We can further make our code more neat by creating another variable and store the greeting message already concatenated with the full name like this:

first_name = “ibrahim”

last_name = “temitope”

full_name = first_name + “ ” + lasr_name

greeting = “Hello ” + full_name.title() + “!”

print (greeting)

The above code will make our work more simpler and easy to read.

Adding whitespace to strings

Whitespace refers to characters that are nonprinting e.g. space, tab, and end of line characters. Whitespace can be used to organize the output of our code so that it is easier for our potential reader user to read.



Adding whitespace with tab and newlines

The tab character gives a four space characters from the left side of the screen. To add a tab to our text, we use the character combination \t. Open the command terminal, launch the python interpreter by typing python into your terminal window and try the following:

>>> print("Hello Python!")

Hello Python!

>>> print("\tHello Python")

Hello Python

A newline character works in the same way as the tab character but instead of adding a four space characters from the left, the output is printed on a new line. To add a newline character, the character combination \n is used. Try the following:

>>> print("Subjects:\nEnglish\nPhysics\nMathematics")

Subjects:

English

Physics

Mathematics

We can also combine tabs and newlines in a single string to suit our formatting. Using “\n\t” informs python to print on a newline and also give a tab character. Try the following:

>>> print (“Subject: \n\tEnglish\n\tPhysics\n\tMathematics”)

            English

            Physics

            Mathematics

Stripping or removing whitespace from a string

Adding extra white space may be confusing in our program. For example, we may see ‘name’ and ‘name ’ to be the same. But in a program, they are two different strings. It is very important to always think about whitespace in our program because sometimes we may want to compare two strings together to check if they are the same or not. If extra whitespace is found, then the program would assume them not to be the same.

Fortunately, python makes it easy to look for and eliminates whitespace in our programs as users enters them into our programs. Python is capable of looking and removing whitespace on both sides of a string.

To ensure that no whitespace exist on the right end of a string, we use the rstrip() method. Let try the code below in our python interpreter:

>>>subject = “English ”

>>>subject

‘English ’

>>>subject.rstrip()

‘English’

>>>subject

‘English ’

The rstrip() method effect on our variable is temporary. To make it permanent, we need to store our stripped value back into our variable

>>>subject = “English ”

>>>subject = subject.rstrip()

>>>subject

‘English’

We can also strip off whitespace from the left of a string using the same convention. But instead of using rstrip() method, we use lstrip() method.

In a case where we are not so sure where the whitespace may be, we can remove whitespace from both sides using the strip() method.

>>>subject = ‘ english ’

>>>subject.lstrip()

‘english ’

>>>subject.rstrip()

‘ english’

>>>subject.strip()

‘english’

That is how we can remove whitespace from a string. Remember that to remove the whitespace permanently, we need to store back the stripped string into our string variable.



Try the following yourself and if you have any problem trying them, drop us a comment as to how we can assist you

1.      Name case: create a variable and store a person name. print the name in uppercase, lowercase and title case.

2.      Print a greeting message to a name stored in a variable. Your message should look like

“Hello Aitem, python is simply interesting”

Your program should have a good readability feature.

3.      Store a location name e.g. Lagos and include a white space at the beginning and end of the location name. print the name once so that the white space is visible, then print the name using each of the stripping functions i.e. lstrip(), rstrip() and strip()

4.      Print the name of ten places you’ve been or wish to be. Include a newline and tab character. Your output should look line:

Places I wished to be:

            Kenya

            United Kingdom

            London

            Etc.
Share:

0 comments:

Post a Comment