Introduction to Python
## Learning Objectives
- Understand what Python is and its applications
- Set up Python development environment
- Write and run your first Python program
- Understand Python syntax basics
## What is Python?
Python is a high-level, interpreted programming language known for its readability and versatility.
```python
# Your first Python program
print("Hello, World!")
```
## Why Python?
- **Easy to learn** - Clear, readable syntax
- **Versatile** - Web, data science, AI, automation, more
- **Powerful** - Rich standard library
- **Community** - Large ecosystem of packages
## Installing Python
### Download
Get Python from
### Verify Installation
```bash
python --version
# or
python3 --version
```
## Running Python
### Interactive Mode
```bash
python
```
```python
>>> print("Hello")
Hello
>>> 2 + 2
4
>>> exit()
```
### Script Mode
Create a file `hello.py`:
```python
# hello.py
print("Hello, World!")
name = input("Enter your name: ")
print(f"Nice to meet you, {name}!")
```
Run it:
```bash
python hello.py
```
### Using IDEs
- VS Code with Python extension
- PyCharm (Community Edition)
- Jupyter Notebook
## Python Syntax Basics
### Comments
```python
# This is a single-line comment
"""
This is a
multi-line comment
(actually a docstring)
"""
'''
Also a
multi-line string
'''
```
### Indentation
Python uses indentation to define code blocks:
```python
if True:
print("Indented") # 4 spaces
if True:
print("Double indented")
else:
print("Else block")
print("Outside") # No indentation
```
### Statements
```python
# Simple statement
x = 5
# Multi-line statement (implicit)
long_string = """
This is a
multi-line string
"""
# Explicit line continuation
total = 1 + 2 + 3 + \
4 + 5 + 6
```
## Variables
```python
x = 5 # Integer
name = "Alice" # String
pi = 3.14 # Float
is_valid = True # Boolean
```
## print() Function
```python
print("Hello") # Hello
print("Hello", "World") # Hello World
print("Value:", 42) # Value: 42
print("x =", x, sep=" -> ") # x = -> 5
print("Line 1\nLine 2") # Line 1, Line 2 (newlines)
print("No newline", end="") # No newline at end
```
## input() Function
```python
name = input("Enter your name: ")
age = input("Enter your age: ")
print(f"{name} is {age} years old")
```
## Your First Program
```python
"""
Simple calculator program
"""
def main():
print("Simple Calculator")
print("-" * 20)
num1 = float(input("First number: "))
num2 = float(input("Second number: "))
print(f"{num1} + {num2} = {num1 + num2}")
print(f"{num1} - {num2} = {num1 - num2}")
print(f"{num1} * {num2} = {num1 * num2}")
if num2 != 0:
print(f"{num1} / {num2} = {num1 / num2}")
if __name__ == "__main__":
main()
```
## Python Versions
- Python 2 (legacy, no longer supported)
- Python 3 (current, use this)
```bash
python3 hello.py # Explicit Python 3
python hello.py # May be Python 2 on some systems
```
## Summary
- Python is a high-level, interpreted language
- Use `print()` to output text
- Use `input()` to get user input
- Python uses indentation for code blocks
- Comments start with `#`
- Save Python files with `.py` extension
- Run with `python filename.py`
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →