Exploring Python Basic Terms

Get to know about python basic terms

Indhumathy Chelliah
Sep 12 · 6 min read

Python basic terms

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:

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.

Image for post
Image for post
Source: Python Documentation

Operators

The following operators are supported in python.

Image for post
Image for post
Source: Python Documentation

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.

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.

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.

Image for post
Image for post

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

Image for post
Image for post
Source: Python documentation

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.

Image for post
Image for post
Source: Python Documentation

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.

Image for post
Image for post
Source: Python Documentation

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.

Image for post
Image for post
Image Source: Author

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.

Image for post
Image for post
Image Source: Author

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

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Indhumathy Chelliah

Written by

Aspiring DataScientist

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Indhumathy Chelliah

Written by

Aspiring DataScientist

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app