Learn Python Fundamentals in 1 hour | Hands-On


Python Programming Language

  • Python is an general-purpose, interpreted, high-level programming language
  • Python has support for Object-Oriented Programming (OOP)
  • Python has many packages/libraries used for developing Machine Learning, Deep Learning, Artificial Intelligence application


  • Artificial Intelligence: Programs with the ability to learn and reason like humans i.e. Robotics works with AI
  • Machine Learning: Algorithms with the ability to learn without being explicitly programmed
  • Deep Learning: Subset of machine learning in which artificial neural networks adapt and learn from vast amounts of data


First thing I want to learn in Python is function called "print"

1
print("Welcome to Python World!!!")

What is variable?

The thing/placeholder to store value of specific data type.

1
2
3
city_name = "Chennai"

print(city_name)

Two important functions one should know in Python

  • type
  • dir
1
2
3
4
5
print(type(city_name))

print("\n")

print(dir(city_name))


Data Types

  • Numeric
    • int
    • float
    • long
    • complex
  • String
  • Boolean
    • True
    • False

Data Type: Numeric

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Plain integers are just positive or negative whole numbers
order_quantity = 5
print(order_quantity)
print(type(order_quantity))

print("\n")

# The float type in Python represents a floating-point number. float values are specified with a decimal point
product_unit_price = 12.50
print(product_unit_price)
print(type(product_unit_price))

print("\n")

# Long integers, they can also be represented in octal and hexadecimal
y = 150L
print(y)
print(type(y))

print("\n")

# Complex numbers are specified as <real part>+<imaginary part>j
x = 5 + 4j
print(x)
print(type(x))
print(x + 3)

Data Type: String

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Strings are sequences of character data

person_first_name = "Arun"
print(person_first_name)
print(type(person_first_name))

person_last_name = "Vijay"

person_full_name = person_first_name + " " + person_last_name
print(person_full_name)
print(type(person_full_name))

Data Type: Boolean

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Objects of Boolean type may have one of two values, True or False

print(True)
print(type(True))

print("\n")

print(False)
print(type(False))

print("\n")

is_run_flag = True
print(is_run_flag)
print(type(is_run_flag))



Data Structures or Data Types

  • list
  • tuple
  • dictionary
  • set

Data Structure: list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Lists are ordered collections of typed objects and can be of any length
# List is mutable, can be modified
# List can contain mixed data types
# List is defined by using brackets []

numbers_list = [1, 5, 2, 4, 7, 3, 6, 6]
print(numbers_list)

print('\n')

names_list = ["Arun", "Arvind", "Vijay", "John"]
print(names_list)

print('\n')

all_list = [2, "Arun", 4, "Arvind", 1, "Vijay", [3, 6, 9], "John"]
print(all_list)
print(type(all_list))

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
marks_list = []

print(marks_list)
print('\n')
# append function will append element in the last position of the list
marks_list.append(80)
print(marks_list)
print('\n')
marks_list.append(50)
print(marks_list)
print('\n')
marks_list.append(35)
print(marks_list)
print('\n')

# insert function will add element in the position/index in the list
marks_list.insert(1, 75)
print(marks_list)
print('\n')
marks_list.remove(50)
print(marks_list)
# remove function will remove element from the list

Data Structure: tuple

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Tuples are a group of values
# Tuple is immutable, can't be changed/modified
# Tuples are defined by using parenthesis ()

numbers_tuple = (1, 5, 10)
print(numbers_tuple)

print('\n')

all_tuple = (1, 'All', 2)
print(all_tuple)

print('\n')
print(all_tuple[-1])

print('\n')
print(all_tuple[0])

print('\n')
print(all_tuple[0:])

print('\n')
print('All' in all_tuple)


Data Structure: dictionary

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Dictionaries in Python are lists of Key:Value pairs
# In Dictionary we can store lot of related information that can be associated through keys
# Dictionaries are created by using braces ({}) with pairs separated by a comma (,) and 
# the key values associated with a colon(:)
# In Dictionaries the Key must be unique

person_d = {"Id": 101, "Name": "Prabhu", "Gender": "Male", "Is_Employed": True}
print(person_d)

print('\n')

fruits_d = {}
fruits_d[1] = 'Apple'
fruits_d[2] = 'Orange'
fruits_d[3] = 'Grapes'
print(fruits_d)
print('\n')
del fruits_d[2]
print(fruits_d)
print('\n')
fruits_d[2] = 'Pomegranate'
fruits_d[4] = 'Mangoes'
print(fruits_d)
print('\n')
print(dir(fruits_d))
print('\n')
fruits_d.pop(1)
print(fruits_d)

Data Structure: set

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Set is an unordered collection of unique items
# Set is defined by values separated by comma inside braces { }

numbers_set = {1, 5, 2, 4, 7, 3, 6, 6}
print(numbers_set)

print('\n')

print(dir(numbers_set))
print('\n')
numbers_set.add(8)
print(numbers_set)

print('\n')
print(numbers_set.remove(7))
print(numbers_set)
print('\n')



if, else, elif Statements and switch-case alternative

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
count = 8
if count == 10:
    print("Number is Ten")
    
is_allowed = True
if is_allowed:
    print("I am allowed!!!")
    
is_allowed = False
if is_allowed:
    print("I am allowed!!!")
else:
    print("I am not allowed!!!")
    
if count == 10:
    print("Number is Ten")
elif count == 8:
    print("Number is Eight")
print('\n')

# No switch-case alternative
def switch_example(number):
    switcher = {
        0: "The Number is Zero",
        1: "The Number is One",
        2: "The Number is Two",
    }
    return switcher.get(number, "Nothing")

number = 2
print(switch_example(number))
number = 3
print(switch_example(number))

Loop Statements

  • for
  • while

Loop Statement: for

1
2
3
4
5
6
7
# The for loop in Python is used to iterate over a sequence (like list, tuple, string) or other iterable objects

marks_list = list([90, 45, 60, 75, 35, 50])
print(marks_list)

for mark in marks_list:
    print(mark)

Loop Statement: while

1
2
3
4
5
6
7
# The while loop in Python is used to iterate over a block of code as long as the given expression (condition) is true.

count = 0

while count < 10:
    count = count + 1
    print(count)



Control Statements

  • break
  • continue
  • pass
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# break
for i in range(10):
    print(i)
    if i == 3:
        break

print('\n')

# continue
for i in range(1, 11):
    if i % 2 == 0:
        print("Number is " + str(i) + " Even")
    else:
        print("Skipping to current iteration")
        continue
    print(i)
   
# pass
number = 10
if number == 10:
    pass


Functions

  • Built-in Functions
  • User Defined Functions
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Built-in Functions
numbers_list = [5, 2, 3, 1, 4]
print(numbers_list)
print(len(numbers_list))
print(min(numbers_list))
print(max(numbers_list))
print(sum(numbers_list))
count = 10
print(type(count))
count_str = str(count)
print(type(count_str))

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# User Defined Functions
def display():
    print("Hello Python!!!")
    
print(display())
print('\n')

def display_message(message):
    print(message)

print(display_message("Python Function!!!"))
print('\n')

def add_numbers(a, b):
    c = a + b
    return c

print(add_numbers(5, 3))

Getting User Input

1
2
3
user_input = input("Please enter the amount: ")
print(user_input)
print(type(user_input))



File Handling

  • text file
  • csv file
  • json file

File Handling: text file

1
2
3
4
5
# Reading Text File

with open('D:\\work\\development\\python\\new_video_python\\hadoop.txt', 'r') as reader:
    for line in reader.readlines():
        print(line)

1
2
3
4
5
6
# Writing Text File

text_file = open('D:\\work\\development\\python\\new_video_python\\sample_file.txt','w')
text_file.write('Programming Python' + '\n')
text_file.write('Programming Scala')
text_file.close()

File Handling: csv file

1
2
3
4
5
6
7
8
# Reading CSV File
# Import required package
import csv

with open('D:\\work\\development\\python\\new_video_python\\people.csv', 'r') as csv_file:
    reader = csv.reader(csv_file)
    for record_line in reader:
        print(record_line)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Writing CSV File
# Import required package
import csv

with open('D:\\work\\development\\python\\new_video_python\\people_new.csv', mode='wb') as csv_file:
    writer = csv.writer(csv_file, delimiter=',')

    #way to write to csv file
    writer.writerow(['Id', 'Name', 'City'])
    writer.writerow(['1', 'John', 'Washington'])
    writer.writerow(['2', 'Martin', 'Los Angeles'])
    writer.writerow(['3', 'Brad', 'Texas'])

File Handling: json file

1
2
3
4
5
6
7
8
# Reading JSON File
# Import required package
import json

with open('D:\\work\\development\\python\\new_video_python\\person.json') as json_file:
  data = json.load(json_file)

print(data)



Object-Oriented Programming (OOP) Support

  • class
  • object
  • inheritance

Object-Oriented Programming: class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Classes are the foundation of object-oriented programming
# Classes represent real-world things you want to model in your programs: for example employees, cars and banks
# You use a class to make objects, which are specific instances of employees, cars and banks
# A class defines the general behavior that a whole category of objects can have, 
# and the information that can be associated with those objects

class User:
    name = ""
    
    # This special function is called Constructor
    def __init__(self, name):
        self.name = name

    def printName(self):
        print("Name  = " + self.name)

Object-Oriented Programming: object

1
2
3
4
# Instance of the class is called object

user_obj = User("Karthik")
user_obj.printName()

Object-Oriented Programming: inheritance

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Inheritance is the capability of one class to derive or inherit the properties from some another class
# One of the major advantages of Object Oriented Programming is re-use. 
# Inheritance is one of the mechanisms to achieve the same. 
# In inheritance, a class (usually called superclass) is inherited by another class (usually called subclass). 
# The subclass adds some attributes to superclass.

# Base class
class User:
    name = ""
    
    # This special function is called Constructor
    def __init__(self, name):
        self.name = name

    def printName(self):
        print("Name  = " + self.name)

# Inherited or Sub class (Note User in bracket) 
class Programmer(User):
    # This special function is called Constructor
    def __init__(self, name):
        self.name = name

    def doPython(self):
        print("Programming Python")

user_obj = User("Arun")
user_obj.printName()

programmer_obj = Programmer("Vijay")
programmer_obj.printName()
programmer_obj.doPython()

Happy Learning!!!

Post a Comment

0 Comments