The Python standard library provides a number of useful functions for working with numbers and numeric data.
- [Instructor] The Python Standard Library provides a rich set of built-in functions for working with numbers. Here in Komodo I've opened a working copy of types.py from chapter 13 of the exercise files. And you notice here down on line four, we're initializing x with a literal string with the digits four and seven in them, and then I'm using the int function and what's interesting about the int function is it's actually not a function it's a constructor for the int type and it'll return an int from another type.
And so when I run this, you'll notice that we're printing out down here on line seven, we're printing the type of x and the value of x and the type of y and the value of y and we see that x is a string and y is an int. And so we're converting a string to an int by calling the int function which is actually the int constructor. Likewise there is a float constructor and when I run this you'll notice that our result y is 47.0 'cause that's the standard way to represent a floating number with a decimal point and it says class float.
I can use the abs function to return the absolute value of a number and so if I say -47 for x, we notice that it's still an integer but we now get the absolute value which is a positive 47. And this also works for floats, if I say -47.3, we get a 47.3 as a result and it is also float. Now if I put this back to a integer number, and I'm going to use the function divmod, which will return both the quotient and the remainder in a tuple of a division.
So if I say x and three, it returns a tuple with two values of 15 and a two, so we know that 15 times three is 45, and that leaves a remainder of two. The complex function again is a type constructor. Complex numbers are two-dimensional numbers. Python uses j instead of i for the imaginary part of the number. Either one of them is standard in different disciplines. This is normal in engineering circles.
So for example if I say y equals x plus seven-three-j, got too many y equals there, and we run this you'll notice that the class is complex and the value is 47 plus 73j so it has the imaginary part of the number in it. You can also create a complex number with the complex constructor which looks like this.
And when I run it we get exactly the same result. Python has a complete set of numeric functions. The documentation has a complete reference here at docs.python.org/3/library/functions.html
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.