Programming Books Python - Home Teachers India

Breaking

Welcome to Home Teachers India

The Passion for Learning needs no Boundaries

Translate

Friday 7 October 2022

Programming Books Python

 Coding, Programmer, Javascript, Computer Science, Artificial Intelligence, Data Science, Softwareengineering, Developer, Web Development, Css, Coder, Linux, Programmers, Webdev, Software, Web Developer, Html, Php, Software Developer, Software Engineer, Html5, Coders, Software Development, Css3, Python Programming, Angular, Mysql, and many more.




Packages and Builtin Functions in PYTHON

Wehavediscussedpackageswithoutreallydescribingwhattheyaresolet’slookatpackages and how it sits within the general setup of Python. As mentioned previously, Python is object orientated which means that everything is an object, you’ll get to understand this in practice, however there are a few important builtin functions which aren’t objects and they are worth mentioning here as they will be used within the book. These builtin types will be used throughout the book so keep an eye out for them. Below we show some useful ones, for a full list refer to  click here

dir(): This function takes in an object and returns the _dir_() of that object giving us the attributes of the object.

>>> name = 'Rob'

>>> dir(name)

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',

'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',

'__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__',

'__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',

'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',

'__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',

'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',

'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha',

'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower',

'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',

'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith',

'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

  float(): Returns a floating point number from an integer of string

>>> x = '1'

>>> float(1)

1.0

  int(): Returns an integer from a float of string

>>> x = '1'

>>> int(1)

1

The Python Book, First Edition. Rob Mastrodomenico.

© 2022 John Wiley & Sons Ltd. Published 2022 by John Wiley & Sons Ltd.

3 Packages and Builtin Functions

  len(): Returns the length of an object

>>> name = 'Rob'

>>> len(name)

3

>>> x = [1, 2, 3, 4]

>>> len(x)

4

  list(): Creates a list from the argument given

>>> name = 'rob'

>>> list(name)

['r', 'o', 'b']

  max(): Gives the maximum value from the argument provided

>>> x = [1, 2, 3, 4]

>>> max(x)

4

>>> name = ['r', 'o', 'b']

>>> max(name)

'r'

  min(): Gives the minimum value from the argument provided

>>> x = [1, 2, 3, 4]

>>> min(x)

1

>>> name = ['r', 'o', 'b']

>>> min(name)

'b'

  print(): Prints the object to the text stream

>>> x = [1, 2, 3, 4]

>>> print(x) [1, 2, 3, 4]

  round(): Rounds the number to a specified precision

>>> y = 1.387668

>>> round(y,2)

1.39

  str(): Converts the object to type string

>>> y = 1.387668

>>> str(y) '1.387668'

3 Packages and Builtin Functions

  type(): Returns the type of an object

>>> y = 1.387668

>>> type(y)

<class 'float'>

  abs(): Returns the absolute value of a numeric value passed in

>>> z = -0.657

>>> abs(z)

0.657

  help(): Gives access to the Python help system >>> help(list)

Help on class list in module builtins:

class list(object)

| list(iterable=(), /)

|

| Built-in mutable sequence.

|

| If no argument is given, the constructor creates a new empty list.

| The argument must be an iterable if specified.

|

| Methods defined here:

|

| __add__(self, value, /) |     Return self+value.

|

| __contains__(self, key, /) |  Return key in self.

|

| __delitem__(self, key, /) |   Delete self[key].

|

| __eq__(self, value, /)

            |     Return self==value.

Now if you are unfamiliar with the Python the concepts used above they will be introduced throughout this book.

Alongside these builtin functions Python also comes with a number of packages. These packages perform specific tasks and are imported into our code. Python has a number of packages that come as default however there are lots of third-party packages which we can also use. In using the Anaconda distribution we get all the default packages as well as the packages that are described previously. We will cover both default and third-party packages throughout this book. To demonstrate this we will introduce how to import a package. The package we are going to introduce is datetime which is part of the standard Python library. What this means is it comes with the Python and is not developed by a third party. Now to import the datetime package you just need to type the following:

>>> import datetime

3 Packages and Builtin Functions

In doing this we now have access to everything within datetime and to see what datetime contains we can run the built in function dir which as we showed earlier gives us the attribute of the object.

>>> import datetime

>>> dir(datetime)

['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__',

'__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime',

'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']

Now if we want to see what these attributes are we use the dot syntax to access attributes of the object. So to see what MINYEAR and MAXYEAR are we can do so as follows.

>>> import datetime

>>> datetime.MAXYEAR

9999

>>> datetime.MINYEAR

1

Now we can import specific parts of a package by using the from syntax as demonstrate below.

>>> from datetime import date

So what this says is from the package datetime import the specific date attribute. This is then the only aspect of datetime that we have access to. This is good practice to only import what you need from a package. Now every time we want to use date we have to call date, in this case its easy enough but you can also give the import an alias which can reduce your code down.

>>> from datetime import date as d

That is the basics of importing packages, throughout this book we will import from various packages as well as show how this same syntax can be used to import our own code. Alongside builtin functions these are key concepts that we will use extensively within this book.

No comments:

Post a Comment

Thank you for Contacting Us.

Post Top Ad