INTRODUCTION TO
PYTHON
PROGRAMMING
PYTHON
Python is a high-level, general-purpose, and very popular
programming language which was created by Guido van
Rassum and released in 1991.
Python programming language (latest Python 3) is being used
in web development, and Machine
Learning, Mathematic and System Scriptting.
Python language is being used by almost all tech-giant companies
like – Google, Amazon, Facebook, Instagram, Dropbox, Uber… etc.
INTRODUCTION TO
PYTHON:
Example of a simple Python program that prints "Hello, World!"
print("Hello, World!")
Python program to add two numbers
a = 10
b=5
result = a + b
print("Sum:", result)
VARIABLE ASSIGNMENT:
In Python, variables are containers used to store data values. Unlike some
languages, Python variables do not require an explicit declaration to reserve
memory space; the assignment happens automatically when a value is assigned.
Assigning integer value to a variable
x = 10
print(x)
Assigning string value to a variable
name = "Alice"
print("Hello,", name)
DATA TYPES IN PYTHON:
Python supports several standard data types like integers, floating-point
numbers, strings, lists, tuples, dictionaries, and more.
Integer data type
age = 25
print(type(age)) # Output: <class 'int'>
# String data type
message = "Hello, Python!"
print(type(message))
Output: <class 'str'>
DATA TYPE CONVERSION
IN PYTHON:
Data type conversion is the process of converting one data type into
another. Python provides built-in functions like
int()
Float()
Str()
# Convert a string to an integer
num_str = "10"
num_int = int(num_str)
print(num_int) # Output: 10
EXAMPLE 2
# Convert an integer to a string
age = 25
age_str = str(age)
print("Age: " + age_str) # Output: Age: 25
GETTING INPUT FROM
THE USER:
Python allows you to get input from the user using the input() function. The
input is always read as a string, so you may need to convert it to the desired
data type.
# Taking input as a string
name = input("Enter your name: ")
print("Hello, " + name)
# Taking input and converting it to an integer
age = int(input("Enter your age: "))
print("Your age is", age)
CONTROL FLOW
STATEMENTS (IF, ELIF,
ELSE):
Control flow statements in Python help direct the execution flow
based on conditions.
If, elif, else
# Check if a number is positive
num = 10
if num > 0:
print("Positive number")
# Check if a number is positive, negative, or zero
num = -5
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
LOOPS IN PYTHON (FOR,
WHILE):
Loops in Python are used to execute a block of code repeatedly,
either a fixed number of times ( for loop) or until a certain condition
is met (while loop).
For Loop:
The for loop iterates over a sequence (like a list, tuple, or string)
and executes a block of code for each element in the sequence.
# Print numbers from 1 to 5 using a for loop
for i in range(1, 6):
print(i)
EXAMPLE 2
# Iterating over a list of names
names = ["Alice", "Bob", "Charlie"]
for name in names:
print("Hello,", name)
WHILE LOOP:
The While loop keeps executing as long as a condition is True
# Print numbers from 1 to 5 using a while loop
i=1
while i <= 5:
print(i)
i += 1
# Summing numbers until the user enters zero
sum = 0
num = int(input("Enter a number (0 to stop): "))
while num != 0:
sum += num
num = int(input("Enter another number (0 to stop): "))
LISTS, TUPLES,
DICTIONARIES, AND SETS:
Python offers several built-in data structures that can store collections
of data.
Lists:
A list is an ordered, mutable collection of elements, defined using
square brackets [].
# Creating and accessing a list
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Output: banana
# Adding and removing elements in a list
fruits.append("orange")
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry', 'orange']
TUPLES:
A tuple is an ordered, immutable collection of elements, defined
using parentheses ().
# Creating a tuple
coordinates = (10, 20)
print(coordinates[0]) # Output: 10
# Tuples are immutable, but they can be unpacked
x, y = coordinates
print(x, y) # Output: 10 20
DICTIONARIES:
A dictionary is an unordered collection of key-value pairs, defined using
curly braces {}.
# Creating and accessing a dictionary
student = {"name": "Alice", "age": 25, "grade": "A"}
print(student["name"]) # Output: Alice
# Adding and removing key-value pairs in a dictionary
student["major"] = "Computer Science"
del student["age"]
print(student) # Output: {'name': 'Alice', 'grade': 'A', 'major': 'Computer Science'}