Pages

Saturday, July 27, 2024

About Python

 About Python


Features of Python:

  • There are no type declarations in Python, the syntax of the expressions you run determines the type of object you create and use.
  • Once an object is created, its operation set is bind for all time—you can perform only string operations on a string and list operations on a list.
  • Python is both dynamically typed and strongly typed.
    • Dynamically Typed attributes : Python keeps track of types for you automatically instead of requiring declaration code.
      • Eg. you don't have to write String value = "Clarity"  as you do in other language, You only need to write value = "Clarity" and Python would automatically keep track of the type for you i.e string type.
    • Strongly Typed attributes : you can perform on an object only operations that are valid for its type.
      • Eg. You can not do append( ) operation on an integer, it can be done on list or any other type of which its part of.




Objects

We are referring Objects every now and then, but what is an Object in Python ? , let's answer this question first.

Objects are essentially just pieces of memory, with values and sets of associated operations.

We can traverse down to objects as follows

Programs are composed of modules ----> Modules contain statements ----> Statements contain expressions ----> Expressions create and process Objects.

Everything in Python is an object.

Objects are:
  • Entities created by Python
  • They have a state i.e Data
  • They have methods i.e Functionality
  • They often represent real world things.

Example of an object representing real world

Person
State (Data)
Name: Harry
Age: 25 years
Citizenship: USA
Height : 5.5 ft
Date of Birth: 26-5-1943

Functionality
Eat ()
Walk ()
Sleep ()
Talk ()

Integers are Objects

It has a state which is the value of the integer
It has functionality.

Few of the functionality of integer objects are:
  • It knows how to represent itself as a string (just for visual output).
  • It knows how to add itself to another integer.

As shown above it has an __add__( ) method which adds it to another method and all this is done behind the scenes.


Mutability and Immutability


Immutability
  • When any operation on an Object does not change its state i.e Data it contains, then that object is called immutable.
  • Immutable Objects in Python are:
    • Numbers
    • String
    • Tuple
    • Boolean
  • Example : String is immutable , where as List is mutable (i.e its state (Data) can be changed in place), lets see example as below:
















































Note : there is no method like ( append ) associated with string object, meaning to say that no method can be associated with immutable object which could change its state (Data).

Mutability
  • When any operation on an Object can change its state i.e Data it contains, then that object is called mutable.
    • Example : List is mutable in Python which is explained in above example.
  • Mutable Objects in Python are:
    • List
    • Dictionary
    • Sets

Sequence

  • They are positionally ordered collection of other objects.
  • It maintains left to right order among the items they contain.
  • Items are stored and fetched by their relative position.
  • Examples of Sequences are
    • Strings --> sequence of one character strings
    • List
    • Tuples
  • As we know that in sequence objects are ordered, so they are indexed and can be fetched with indexing expressions as shown below:





































Note : At any place where Python expects a value, we can use literal, a variable or any expression.

Slicing

  • It is a way to extract something from a sequence.
  • In an above example we have extracted strings from strings using slicing.
  • Its general syntax is : S[I:J] which means:
    • Return us everything from S from I'th position to J'th position, where I is included and J'th position is excluded, as evident in the above example.
  • The result of slicing is returned in a New object.


Getting Help in Python

  • There is a built in dir() function, which returns a list of attributes/method names available for a given object as shown in below example.

>>> s = 'Clarity'

>>> s

'Clarity'

>>> type(s)

<class 'str'>

>>> dir(s)

['__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', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']


  • To gain more insight into what the function actually does there is a help() function available in Python as shown in below example.
Python : Help : Function
































































No comments:

Post a Comment

Python : About : Variables

  Variables in Python Variables are used to: Name an object. Remind us about the usability of an object. Allow us to reuse same object in mu...