Python - Introduction

Overview of Python’s history

Python is a widely-used high-level programming language known for its simplicity, readability, and versatility. Its development can be traced back to the late 1980s and early 1990s.

Python has gained immense popularity due to its versatility and wide range of applications.

Features of Python:

  1. Readability: Python has a clean and easily readable syntax, which makes it highly approachable for beginners and encourages the use of good coding practices.

  2. Easy to Learn and Use: Python has a gentle learning curve, making it an excellent choice for beginners. Its simplicity and English-like syntax contribute to its ease of use.

  3. Expressive Language: Python allows developers to express concepts and ideas in fewer lines of code compared to other languages. This leads to increased productivity and better code readability.

  4. Cross-platform Compatibility: Python is available on various platforms such as Windows, macOS, Linux, and many others. This enables developers to write code once and run it on different platforms without major modifications.

  5. Large Standard Library: Python comes with a comprehensive standard library that provides a rich set of modules and functions for various tasks, such as string manipulation, file I/O, networking, and more. This reduces the need for external libraries for many common programming tasks.

  6. Third-Party Libraries: Python has a vast ecosystem of third-party libraries and frameworks that extend its capabilities. These libraries cover a wide range of domains, including data analysis, web development, machine learning, scientific computing, and more.

  7. Dynamically Typed: Python is dynamically typed, which means you don’t need to explicitly declare variable types. This allows for flexibility and faster development cycles.

  8. Object-Oriented Programming (OOP) Support: Python supports object-oriented programming principles, such as classes, objects, inheritance, and polymorphism. It enables developers to write modular and reusable code.

  9. Interpreted Language: Python uses an interpreter-based approach, which allows for rapid development and testing. You can run Python code directly without the need for compilation, making it suitable for scripting and prototyping.

  10. Community and Support: Python has a vibrant and supportive community of developers worldwide. This community actively contributes to the language, creates open-source libraries, and provides help through forums, tutorials, and documentation.

 

 

Python - Basic Syntax and Data Types:

Variables in Python:

In Python, variables are used to store and manipulate data. They act as named containers that hold values of different types, such as numbers, strings, lists, or objects. Here’s an explanation of variables in Python, along with a simple example, syntax, and rules:

Syntax:

variable_name = value

Example:

# Assigning a value to a variable

age = 25

# Printing the value of the variable
print(age) # 

Output: 25

# Modifying the value of the variable
age = age + 1

# Printing the updated value
print(age) # 

Output: 26

Explanation:

  • The variable_name is the name given to the variable. It should follow certain rules (explained below).
  • The = symbol is the assignment operator, used to assign a value to the variable.
  • The value represents the data that the variable will hold. It can be a literal value (e.g., numbers, strings) or an expression.

Rules for Variable Names in Python:

  • Variable names can consist of letters (both uppercase and lowercase), digits, and underscores (_).
  • The first character of a variable name cannot be a digit.
  • Variable names are case-sensitive, meaning age, Age, and AGE would be considered different variables.
  • Variable names should be descriptive and meaningful to enhance code readability.
  • It is recommended to use lowercase letters and underscores for variable names (e.g., age, student_name) to follow Python’s naming conventions.
  • Python keywords (reserved words such as if, for, while, etc.) cannot be used as variable names.

 Data-Types in Python:

In Python, data types represent the kind of values that variables can hold. Python provides several built-in data types to handle different types of data. Here’s an explanation of some commonly used data types in Python, along with simple examples and syntax:

1.Numeric Types:

  • int: Represents integer values (whole numbers) without decimal points.

    Example: age = 25

  • float: Represents floating-point values (decimal numbers).

    Example: height = 1.75

2.String:

  • str: Represents a sequence of characters enclosed in single quotes ('...') or double quotes ("...").

    Example:  name = “John Doe”

3. Boolean:

  • bool: Represents either True or False, indicating the truth value of a condition.

    Example: is_student = True

4. List:

  • list: Represents an ordered collection of items, enclosed in square brackets ([]), separated by commas. Lists can contain elements of different data types.

    Example: fruits = [“apple”, “banana”, “orange”]

5. Tuple:
  • tuple: Similar to lists, but they are immutable (cannot be modified once created). Tuples are enclosed in parentheses (()).

    Example: coordinates = (3, 4)

6. Dictionary:

  • dict: Represents a collection of key-value pairs enclosed in curly braces ({}). Each key is associated with a value.

    Example: person = {“name”: “John Doe”, “age”: 25, “is_student”: True}

7. Set:

  • set: Represents an unordered collection of unique elements, enclosed in curly braces ({}). Sets are useful for operations like union, intersection, and difference.

    Example: unique_numbers = {1, 2, 3, 4, 5}