Introduction to Python
K Harisai,
Executive Committee Member, Swecha,
Senior Software Engineer, Syncoffice
Agenda
● What is python and Why Python
● Variables and Data Types
● Operators
● Control Flow Statements
● Functions
● Lists, Tuples, Dictionaries
● File Handling
● Libraries and Modules
What is Python
● Python is an interpreted, high level, general purpose programming language.
● Created by Guido van Rossum and first released in 1991.
● Python is a programming language that lets you work quickly and integrate systems more
effectively.
● Uses:
○ Web development
○ Software development
○ Mathematics
○ System scripting
○ Machine Learning
Why Python?
● Easy to learn
● Versatile(can be used for developing wide range of applications)
● Large Community
● Cross platform Compatibility
● Interpreted Language
● Great for Rapid Prototyping
Image Source: IEEE Spectrum
Installing Python
● Sudo apt install python3
● To install python packages follow these steps
○ Sudo apt install python3-pip
○ Pip install <Package Name>
● https://www.python.org/downloads/
Running Python Code
● There are several ways to run the python code on different development
environment.
● There are 3 types of environments.
– Text editors.
– Full IDEs.
– Notebook environments.
Python syntax
● Python is designed to be a highly readable programming language
● Unlike other programming languages, python uses “Indentation”
○ Ex:
Variables and Datatypes
● my_string = “Hello World”
● my_number = 12
● my_float = 12.50
● my_boolean = True
Lists, Tuples, and Dictionaries
* Lists are Ordered Collection Items, and they are mutable, Whereas Tuples are
Immutable
# List # Dictionary
my_list = ["apple", "banana", "cherry"] my_dict = {"name": "John", "age": 30, "city": "New
York"}
print(my_list[1])
print(my_dict["age"])
# Tuple
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[1])
Operators
# Arithmetic Operators # Comparison Operators # Logical Operators
a=5 a=5 a = True
b=3 b=3 b = False
print(a + b) print(a > b) print(a and b)
print(a - b) print(a < b) print(a or b)
print(a * b) print(a == b) print(not a)
print(a / b) print(a != b)
print(a % b)
Control Flow Statements:
# If Statement # For Loop # While Loop
x=5 x=0
for i in range(5):
if x > 0:
print(i) while x < 5:
print("x is positive")
print(x)
elif x < 0:
x += 1
print("x is negative")
else:
print("x is zero")
Functions:
def say_hello(name):
print("Hello, " + name + "!")
say_hello("John")
File Handling
# Read from a file # Write to a file
with open("myfile.txt", "r") as file: with open("myfile.txt", "w") as
file:
data = file.read()
file.write("Hello, World!")
print(data)
Libraries and Modules
# Creating a custom module
#Importing the math
Library # mymodule.py
def say_hello(name):
import math print("Hello, " + name + "!")
print(math.sqrt(16))
# main.py
import mymodule
mymodule.say_hello("John")
Python Oops
Questions