This document provides a basic introduction to Python, covering fundamental concepts and syntax. It's intended for individuals new to programming or those seeking a starting point with Python.
Python is a high-level, interpreted, general-purpose programming language known for its readability, versatility, and extensive libraries. It is widely used in various fields, including web development, data science, machine learning, scripting, and more.
To begin working with Python, you need a Python interpreter. This can be installed through the official Python website. Once installed, you can open a terminal or command prompt and type python
to access the interactive interpreter.
Python uses a simple and consistent syntax:
- Variables: Variables store data and are declared using a name followed by an assignment operator (`=`).
name = "Alice"
age = 30 - Data Types: Python supports various data types:
- Numbers: Integers (`int`), floating-point numbers (`float`)
- Strings: Text enclosed in single (`'`) or double (`"`) quotes.
- Booleans: `True` or `False`.
- Lists: Ordered sequences of elements.
- Dictionaries: Key-value pairs.
- Operators: Operators perform operations on data:
- Arithmetic operators: `+`, `-`, `*`, `/`, `%`, `//` (floor division)
- Comparison operators: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical operators: `and`, `or`, `not`
- Conditional Statements:
- `if` statement: Executes code block if a condition is true.
- `elif` statement: Executes code block if the previous `if` condition is false and the current condition is true.
- `else` statement: Executes code block if all previous conditions are false.
if age >= 18:
print("You are an adult"
elif age >= 13: print("You are a teenager") else:
print("You are a child") - Loops:
- `for` loop: Iterates over a sequence of elements.
- `while` loop: Executes code block repeatedly as long as a condition is true.
for i in range(5):
print(i)i = 0
while i < 5:
print(i)
i += 1
Functions are blocks of reusable code that perform specific tasks. They are defined using the `def` keyword:
def greet(name):
"""Greets a user with their name."""
print("Hello, " + name + "!")
greet("Bob")
Python's strength lies in its extensive libraries. Modules are files containing code that can be imported and used in other programs.
import math
print(math, sqrt(25))
This code imports the `math` module and uses its `sqrt` function to calculate the square root of 25.
This document provided a basic overview of Python. For more advanced concepts and applications, explore resources such as:
- Official Python Documentation
- Online Tutorials
- Python Community Forums:
With dedication and practice, you can master the art of programming in Python and unleash its potential in various domains.