Get to know about python basic terms

Python basic terms
- Identifiers
- keywords
- Operators
- Object Id,Object Type
- Sequences
- Set Types
- Mappings
- Iterable
- Iterator
- Simple statements
- Compound statements
- Built-in types
- Built-in functions
- Built-in methods
- Mutable, Immutable
- Comments
- Docstring
Identifiers
Python identifiers are names given to identify variables, class, function, module, packages, methods, instance variables, or any other object.
Identifiers start with lowercase or uppercase letter A-Z a-z
, or underscore
.
It can then followed by zero or more letters, digits, and underscore.
Special characters and space are not allowed in identifiers.
Naming Convention:
- If an identifier starts with an underscore, then it is a private identifier
Refer to my story for underscores in python. - Class names should normally use the CapWords convention.
class BankAccount:
- Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
- Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
- Function names should be lowercase, with words separated by underscores as necessary to improve readability.
def withdrawal():
def bank_withdrawal():
- Variable names follow the same convention as function names.
a=[1,2,3]
d={‘a’:1}
colors=[‘red’,’blue’]
- Method names and also instance variables should be lowercase with words separated by underscores as necessary to improve readability.
- Use one leading underscore only for non-public methods and instance variables.
Names to avoid:
Never use the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter oh), or ‘I’ (uppercase letter eye) as single-character variable names. In some fonts, these characters are indistinguishable from the numerals one and zero.
Example 1: Space is given in the function name
def bank withdrawal(): pass #Output:SyntaxError: invalid syntax
Example 2:Special characters given in function name
def withdrawal%(): pass
#Output:SyntaxError: invalid syntax
Keywords:
Keywords are reserved words that cannot be used as ordinary identifiers.

Operators
The following operators are supported in python.

Object Id,Object Type
Every object has an identity, type, and value.
Object Id is the object address in the memory.id()
function returns the object’s identity
Object type determines the type of the object like list,dict, tuple, string etc.type()
function returns the object type
list1=[1,2,3] print (id(list1))#Output:39270408 print (type(list1))#Output:<class 'list'> tuple1=(1,2,3) print (id(tuple1))#Output:39405192 print (type(tuple1))#Output:<class 'tuple'> dict1={1:'a',2:'b'} print (id(dict1))#Output:22432336 print (type(dict1))#Output:<class 'dict'>
Sequences:
Sequences represent finite ordered set indexed by non-negative numbers.
- Immutable Sequences
Strings
Tuples
Bytes
str1="python" print (str1[1])#Output: y tup1=(1,2,3) print (tup1[1])#Output:2
2. Mutable Sequences
Lists
ByteArrays
lis1=[1,2,3] print (lis1[2]) #Output:3
Set Types
Set types represent unordered, finite sets of unique, immutable objects. Common uses for sets is to remove duplicates from the sequence and perform mathematical set operations like union, intersection, difference, symmetric difference.
- Sets
Sets are immutable. Created by using a built-in set() constructor.Empty set is given asset()
- Frozen sets
Frozen sets are immutable. Since it is immutable and also hashable, it can be used as dictionary keys
set1=set((1,2,3)) print (set1)#Output:{1,2,3} set2={1,2,3} print (set2)#Output:{1,2,3} fro1=frozenset({1,2,3}) print (fro1)#Output:frozenset({1, 2, 3})
Mappings
A mapping object maps values of one type (the key type) to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. A dictionary’s keys are almost arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity.
d={'a':1,'b':2}
Iterables
An object capable of returning its members one at a time. Iterables can be iterated upon using for loop.
Example. List,set,tuple,dict,range object,file objects
list1=[1,2,3] for i in list1: print(i , end=" ") #Output:1 2 3 set1={1,2,3} for i in set1: print(i , end=" ")#Output:1 2 3 tuple1=(1,2,3) for i in tuple1: print(i , end=" ")#Output:1 2 3 dict1={1:'a',2:'b'} for i in dict1: print (i,end=" ")#Output:1 2 3 r=range(1,4) for i in r: print (i,end=" ")#Outpur:1 2 3
Iterators
An object representing a stream of data. If an iterable contains the iter()
function, we can convert to an iterator.

We can iterate over iterators
1.Using for loop
We can iterate over the iterator. But it will iterate one time only. Attempting to iterate second time will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
n1=[1,2,3] n2=iter(n1) for i in n2: print (i, end=" ") #Output: 1 2 3 print ("")
#exhausted iterator returns an empty container n3=list(n2) print (n3)#Output: []
2.Using next() function
When the iterator is exhausted( no more data available) a StopIteration
exception is raised instead.
n1=[1,2,3] n2=iter(n1) print (next(n2))#Output: 1 print (next(n2))#Output: 2 print (next(n2))#Output: 3 print (next(n2))#Output:StopIteration
Refer to my story Iterable vs Iterator
Simple Statements
A simple statement is comprised of a single logical line

Compound Statements
Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.
A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause.

Built-in types
The principal built-in types are numerics, sequences, mappings, classes, instances, and exceptions.
Built-in functions
The Python interpreter has a number of built-in functions that are always available. A built-in function object is a wrapper around a C function. The number and type of arguments are determined by the C function.

Built-in methods
Built-in methods on mutable objects.
Methods are called on an object. Its return type is None. It will modify the original object itself. But python function doesn’t modify the object, it will return a new object.
Ex. List object has a built-in method sort
and built-in function sorted()
sort →This has to be called on an object (list) like list.sort()
It will modify the original list. It won’t return anything.
sorted() -This is built in function. sorted(list)
It will return a new sorted list. It won’t modify the original list.
#built-in methods list1=[2,1,5,3,7,6,4] print (list1.sort())#Output:None print (list1)#Output:[1,2,3,4,5,6,7] #built-in functions list2=[2,1,5,3,7,6,4] print (sorted(list2)) #Output:[1,2,3,4,5,6,7] # It won't modify the original list. print (list2)#Output:[2, 1, 5, 3, 7, 6, 4]
Built-in methods supported by list, tuple,dict, and set.

Built-in methods supported by strings
Built-in methods on immutable objects like string will return a new copy of the string. It won’t modify the original string.

Mutable objects
Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.
Mutable objects -List,dictionary,set
Immutable objects -Strings, tuple
Comment
Comments in Python start with # and continue until the end of the line. Comments are important because it will improve the readability of the code. Comments in the program are for humans to read and understand and not for the computer to execute.
# sorting the given list colors=['red','blue','yellow'] print (sorted(colors))
DocString
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It allows programmers to add quick notes about the function/class. It can span multiple lines also. We can access the docstring using the __doc__ method.
For consistency, always use “””triple double quotes””” around docstrings.
def add1(a,b): """This function used to calculate subtraction of 2 numbers""" return a-b print(add1.__doc__) #Output: This function used to calculate subtraction of 2 numbers
Resources(Python Documentation):
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee