Python has functions, and we've seen one of them already, that perform type conversion. Converting from one type of data to another. For example, we saw that if we want to perform floating point division, and we have two integers, Python will automatically perform integer division, truncating anything on the right side of the decimal point, unless we tell Python that we'd like to perform floating point division. We tell Python that we'd like to do floating point division by converting one, or both, of the numbers to a floating point number.
In the example above, the answer to 20/3 would result in the number 6. However, if we use the function float(), then we get the right answer.
The float() function normally works to convert integers to floating point, or to extract floating point numbers from strings. In the example above, the integer 7 is converted to 7.0. The string "7" is converted to the floating point number 7.0. Two things happened to the string. First, the number was extracted from the sequence of characters. And secondly, the number was converted.
The float() function will also convert floating point numbers. However, look at the last conversion. When trying to convert the string "jack" to a number, we get an error message. The string "jack" doesn't contain a valid number that can be converted.
The str() function converts its argument to a string.
In the examples above, the str() function takes the integer, 1, and returns the string '1'. Similarly, it takes the floating point number 1.1 and returns the string '1.1'
The str() function will also convert strings, back to strings. This might be useful in the case where you're writing data to a file and don't want to test each type before writing it out. You can be sure that the str() function will faithfully take your strings and write them unchanged.
Notice the statement:
'jack' + 1
This results in a Python error. Python does not know what to do. In the next statement:
'jack' + str(1)
Python converts the integer, 1, to a string first, now python knows that we need to use the string concatenation operator, the "+", to concatenate the string "Jack" to the new string "1".
Python allows you to create your own functions. To create your own functions, you prototype them as follows:
def function_name(parameter_list):
body of function
Here's an example where we define a function.
The function name is sayHi. This function has no parameters, but the parentheses are important. The function body has a single line of code. The line prints a string, "Hello World!".
When we call the function, we simply use it's name with the arguments it requires.
Let's redefine the function so that it takes an argument. In this case, an argument that tells it how many times to say hello.
Now when we call the function sayHi(), we need to pass an argument. The argument will be used to repeat the string "Hello World! " a number of times. Note the space after the exclamation mark in the string "Hello World! ".
Arguments versus Parameters
In most programming language texts, the word argument and parameter are used interchangeably, but there is a difference. The parameter is defined in the function prototype. In our example above, "n" is a parameter defined in the prototype:
def sayHi(n)
Each time we call the function sayHi() we pass an argument. In our example above, the numbers, 3, 2 and zero (0) are arguments.
Good work. keep this work up.
ReplyDeletePython Online Training
Data Science Online Training