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.
- Entities created by Python
- They have a state i.e Data
- They have methods i.e Functionality
- They often represent real world things.
- 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:
- 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
- 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.





No comments:
Post a Comment