Python
- For Data Engineering
What is Python?
• Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991.
• It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
Where all we use Python?
• Python can be used on a server to create web
applications.
• Python can be used alongside software to create
workflows.
• Python can connect to database systems. It can also
read and modify files.
• Python can be used to handle big data and perform
complex mathematics.
• Python can be used for rapid prototyping, or for
production-ready software development.
Intro to Python - Syntax
Indentation:
• Indentation refers to the spaces at the beginning of a code line.
• We should be using either space or tab while using functions, loops and conditional statements.
Variables:
• Python variables doesn’t need to be embedded with datatype at beginning as python has that
capacity to capture the datatype based on the value assigned.
Example: a=10
• In above example a is variable, python will assign this variable as int datatype. A is the variable.
• Assigning multiple values at the same time.
x,y,z=1,2,3 #This assigns x=1, y=2 and z=3
DataTypes
Text Type: Str
Numeric int, float, complex
Types:
Sequence list, tuple, range
Types:
Mapping dict
Type:
Set Types: set, frozenset
Boolean bool
Type:
Binary bytes, bytearray, memoryview
Types:
None NoneType
Type:
Casting and checking datatype
• If you want to specify the data type of a variable, this
can be done with casting.
Example: Let’s cast a integer to string.
X=str(1) #output: ‘1’
• To check the type of variable:
Print(type(X)) #Output: String
Global and static variables
• Any variable assigned within a block of code is a static variable.
Global:
name=“venkat”
for I in name:
print(i)
Static:
Def exp():
name=“venkat”
for I in name:
print(i)
Strings
• We can create a string variable either in single or double quotes.
Example: a=‘hello’
a=“hello”
Multi line:
A=
“””
I am
Learning Python
”””
Slicing and looping through strings
• Strings work like arrays and we can loop through them.
A=“HELLO”
For I in A:
print(A)
Output:
H
E
L
L
O
Slicing: print(A[2])
Output: L
String position starts from 0
Lists
• List are one kind of arrays which store elements.
• They have the ability to store any type of data element.
• We can add, remove, iterate through element in lists.
• List items are ordered, changeable, and allow duplicate values.
Syntax: list_name=[elements]
Below are functions in list
• insert(index, val), append(val), extend(list), remove(val), pop, del
• Nested List
Tuples
• Tuples are used to store multiple items in a single variable.
• Syntax: Tup_name=(item1,item2,---)
• Tuple items are ordered, unchangeable, and allow duplicate values.
• Tuple items are indexed and it starts with 0.
• When we say that tuples are ordered, it means that the items have a defined order, and that
order will not change.
• Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple
has been created.
Sets
set_name={}
• Sets are used to store multiple items in a single variable.
• Set items are unordered, unchangeable, and do not allow
duplicate values.
• Unordered means that the items in a set do not have a defined
order.
• Set items can appear in a different order every time you use
them, and cannot be referred to by index or key.
• Set items are unchangeable, meaning that we cannot change
the items after the set has been created.
• Sets cannot have two items with the same value.
Dictionary
• Dictionaries are used to store data values in key:value
pairs.
• A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.
Syntax: dict()
Dictionary={item:val}
Conditional statements
If condition:
statements
Elif condition:
statements
Else:
statements
Loops
• While
• For
• List comprehensions
Functions
Def fun_name(parameters):
statements
return
Types of arguments:
1. Parameters – fixed
2. Parameter with input value
3. Arbitrary arguments – not sure on number of parameters