You are on page 1of 21

TATA CONSULTANCY SERVICES

PYTHON
RECAP
Ashwin Joy | Technical Faculty | TD Kochi
BASICS
Printing data
Numbers and strings
Variables
Getting user input
Writing comments
OPERATORS
Arithmetic + - * / %
Increment & decrement ++ --
Relational == != < > <= >=
Logical and or not
Assignment =
CONDITIONAL
STATEMENTS
if statement
else statement
elif statement
indentation in python
LOOPS
Loops allow us to execute a statement or group of statements multiple times. Thus we can
reduce the burden of repeatedly writing a piece of code many times.

while loop
for loop
FUNCTIONS
A function is a block of code which only runs when it is called. You can pass data, known as
parameters, into a function. A function can return data as a result.

in-built functions
user defined functions
STRINGS
A string is created by entering a text between two single or double-quotes.

String concatenation
String slicing
String splitting
LISTS
If we want to store a set of items in an arranged order, we can use lists in Python. We can
create a list by using square brackets with items inside. We can use commas to separate
each item.

Accessing items in a list


List functions - append, insert, len
MODULES
Consider a module to be the same as a code library. A file containing a set of functions
you want to include in your application. eg: turtle, math, random, datetime

The modules that are pre-installed with Python constitute the standard library of Python.
DICTIONARIES
Dictionaries are a special type of data structure in python. They are used to store key-value
pairs.

Accessing elements in a dictionary


Adding new items to a dictionary
Looping through a dictionary
TUPLES
Tuples are very similar to lists, except that they are immutable, that is, they can’t be
changed once defined. Also, they are represented using parentheses rather than square
brackets.

Examples of tuples
Mutable vs immutable data types
SETS

A set is an unordered collection of items.


All the elements in a set are unique and immutable.
However, the set itself is mutable. We can insert or delete items from it if we want.
Sets can also perform mathematical set operations like union, intersection, symmetric
difference, etc.
STRING FORMATTING
String formatting provides a powerful way to combine non-strings within strings. It uses the
format() method to substitute a number of arguments in the string.

Example:

print("{0}{1}{0}".format("abra","cad"))
EXCEPTION HANDLING
An exception is an event that occurs due to incorrect code or input. We can handle
exceptions in Python using try-except statements.

Types of exceptions/errors.
The try-except block
The finally statement
FILE HANDLING
We can use python to read and write the contents of files.

Opening files
Reading files
Writing to files
REGEX
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
RegEx can be used to check if a string contains the specified search pattern.

Example:

import re
txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)
OOP CONCEPTS
Object-oriented programming (OOP) is a programming paradigm based on the concept of
objects and data. We treat every entity as an object in OOP.Just like we call everything we
see in real-life as objects, we call every entity in OOP as objects.

Class
Methods
The __init__ method
INHERITANCE
The process of deriving a new class from an old class (parent class) is known as inheritance.
The old class is called the superclass and the new one is called the subclass. The subclass
can use all the stuff that is inside a superclass.
POLYMORPHISM
Polymorphism is the property by which objects belonging to different classes
are able to respond to the same message but in different ways.

Polymorphism can be carried out through inheritance, with subclasses


making use of superclass methods or overrides them so that the same
method works differently based on the object that we are working with.
ENCAPSULATION

Encapsulation is used to restrict access to methods and variables. The code and
data are enclosed within a single unit, just like a capsule, from being modified
by external agents.
THANK YOU

Ashwin Joy
Technical Faculty - Talent Development Kochi
Tata Consultancy Services Ltd.

You might also like