Introduction to Python Programming
1. Introduction to Python
Python is a high-level, interpreted programming language designed for simplicity and readability. It was
created by Guido van Rossum and released in 1991. Python supports multiple programming paradigms
including procedural, object-oriented, and functional programming.
Key features of Python:
- Simple syntax similar to English
- Interpreted language, no need for compilation
- Large standard library and vast ecosystem of third-party packages
- Supports multiple platforms (cross-platform)
- Used in web development, data science, automation, AI, machine learning, etc.
Python's ease of learning and versatility make it an ideal choice for both beginners and professionals.
Python is often used as a first programming language due to its clean syntax and wide support for various
applications. Python files use the `.py` extension, and the interpreter can be run interactively or via scripts.
Python comes with a large standard library that includes modules for file I/O, system calls, sockets, and even
Internet protocols. It also has robust community-contributed libraries such as NumPy, pandas, Flask, Django,
TensorFlow, and more.
Use the official Python website (https://python.org) to download and install Python. Tools like Jupyter
Notebooks or IDEs like PyCharm, VS Code, and Thonny make Python development easier.
2. Variables and Data Types
Variables are containers for storing data values. In Python, variables are created when first assigned a value.
Python uses dynamic typing, so you don't have to declare the type explicitly.
Example:
x=5 # integer
Page 1
Introduction to Python Programming
y = 3.14 # float
name = "Alice" # string
is_active = True # boolean
Data Types:
- int: Whole numbers
- float: Numbers with decimals
- str: Text values enclosed in quotes
- bool: True or False
- list, tuple, dict, set: Collections (introduced later)
Variable Naming Rules:
- Can include letters, numbers, and underscores
- Cannot start with a number
- Case-sensitive (e.g., Age and age are different)
Type Conversion:
Python provides functions like int(), float(), str(), and bool() to convert between types.
Example:
age = "25"
age = int(age) # converts string to integer
Variables can also be collections like:
- List: Ordered, mutable collection [1, 2, 3]
- Tuple: Ordered, immutable collection (1, 2, 3)
- Set: Unordered, unique values {1, 2, 3}
- Dictionary: Key-value pairs {"name": "Alice", "age": 25}
You can check the type of a variable using `type()` and delete variables using `del`.
Page 2
Introduction to Python Programming
Example:
a = 10
print(type(a)) # <class 'int'>
del a
3. Input from User
Python allows user interaction using the input() function. It halts program execution and waits for the user to
enter input.
Syntax:
input("Prompt message")
Example:
name = input("Enter your name: ")
print("Hello", name)
All input is treated as string by default. You need to convert it for numerical operations.
Example:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
print("Sum is", num1 + num2)
Always validate and handle inputs using error handling (try-except) for robust programs.
You can also handle input errors using try-except to prevent crashes due to invalid input.
Example:
try:
num = int(input("Enter a number: "))
Page 3
Introduction to Python Programming
print("Square is", num ** 2)
except ValueError:
print("Invalid input, please enter a number.")
This is known as exception handling and is vital for building robust applications.
4. Your First Program
Your First Program
In Python, you can create and run your first simple program using the print() function.
Example:
print("Hello world!")
This line displays the text "Hello world!" in the console. It is your first interaction with Python, and it's often
referred to as the "Hello World" program in programming.
5. Dissecting Your Program
Dissecting the Hello World Program
- print(): This is a built-in function in Python that displays the value you pass to it on the screen.
- "Hello world!": This is a string, a type of value that holds text.
- Strings in Python must be enclosed in quotes (single or double).
You can also print numbers and perform calculations directly:
print(2 + 3) # Outputs: 5
print("Hello" + " World!") # String concatenation
print("Spam" * 3) # String replication
6. String Concatenation and Replication
Page 4
Introduction to Python Programming
String Concatenation and Replication
Concatenation: Joining strings using the + operator
Example:
greeting = "Hello" + " " + "world"
print(greeting) # Outputs: Hello world
Replication: Repeating a string using the * operator
Example:
laugh = "Ha" * 3
print(laugh) # Outputs: HaHaHa
These are useful in creating repeated patterns or combining messages.
7. Example Program: Print, Input, len()
Example Program: Personalized Greeting Generator
This program will ask the user for their name and a message, and then print a customized greeting. It also
shows how to use the `len()` function to find the number of characters in the message.
Example:
print("Welcome to the Greeting Generator!")
name = input("What is your name? ")
message = input("Type a short message: ")
print("Hello " + name + "!")
print("Your message is: " + message)
print("Your message has", len(message), "characters.")
Page 5
Introduction to Python Programming
Explanation:
- input() is used to collect the user's name and message.
- print() is used to display information on the screen.
- len() returns the number of characters in the message string.
Sample Output:
Welcome to the Greeting Generator!
What is your name? Alice
Type a short message: Have a great day!
Hello Alice!
Your message is: Have a great day!
Your message has 18 characters.
Page 6