Chapter 1: Introduction, Variables, and Data Types notes, Polytechnic 3rd semester SCRIPTING LANGUAGE (PYTHON) notes

1.1 History

Python was created by Guido van Rossum in the late 1980s and released in 1991. It was designed with a focus on code readability, making it an easy-to-learn language for beginners. Python has evolved through various versions, with Python 3 being the most widely used today.

Perl, created by Larry Wall in 1987, is a high-level, general-purpose programming language known for its text processing capabilities. Though Perl's popularity has declined, it is still used in system administration and network programming.


1.2 Features

  • Easy to Read and Write: Python's syntax resembles plain English, which makes it highly readable.
  • Interpreted Language: Python executes code line by line, which allows for easy debugging.
  • Dynamically Typed: Variables do not require an explicit declaration of data type.
  • Large Standard Library: Python has extensive libraries for various tasks like file handling, mathematics, and web development.
  • Cross-Platform: Python is portable and can run on multiple operating systems, including Windows, macOS, and Linux.
  • Community Support: Python has a massive community, making it easy to find support and libraries for different use cases.

1.3 Setting up Path

To use Python from the command line, it’s essential to set the path to the Python interpreter.

  1. For Windows:
    • Go to System Properties > Advanced > Environment Variables.
    • Under the System Variables, find the Path variable and click Edit.
    • Add the path to the Python installation directory.
  2. For macOS/Linux:
    • Use the terminal to open .bashrc or .zshrc file.
    • Add export PATH="/usr/local/bin/python3:$PATH" to the file and save.

1.4 Installation and Working with Python/Perl

  • Python Installation:
    1. Download the latest version from python.org.
    2. Run the installer and ensure the option to add Python to PATH is selected.
    3. Verify installation by typing python --version in the terminal.
  • Perl Installation:
    • Download from perl.org.
    • Install via ActivePerl or Strawberry Perl for Windows.

Working with Python:

  • You can write Python code in any text editor (e.g., VS Code, Sublime) or use the Python IDLE.
  • To run a script, use python filename.py in the terminal.

1.5 Basic Syntax

Python's syntax is simple and clean. The key points include:

  • Indentation: Python uses indentation (whitespace) to define code blocks instead of curly braces.
    • Example:
      python

      if x > 5: print("x is greater than 5")
  • Comments: Single-line comments start with #, and multi-line comments can be enclosed in """.
  • Case Sensitivity: Python is case-sensitive, meaning Var and var are treated as different variables.

1.6 Understanding Python Variables

Variables in Python are dynamically typed, meaning their data type is determined during runtime.

  • Declaration: Variables are created when they are assigned a value.
    • Example: x = 10
  • Reassignment: Variables can change type based on the value they hold.
    • Example: x = "Hello" (changes x to a string)

1.7 Numeric Data Types

Python supports several types of numeric data:

  • int: Integer numbers, e.g., 10
  • float: Decimal numbers, e.g., 10.5
  • complex: Numbers with a real and imaginary part, e.g., 2 + 3j

Operations on these types include addition (+), subtraction (-), multiplication (*), and division (/).


1.8 Using String Data Type and String Operations

Strings in Python are sequences of characters enclosed in quotes, either single (') or double (").

  • Creating Strings: name = "Alice"
  • String Concatenation: Strings can be joined using the + operator.
    • Example: "Hello " + "World" results in "Hello World".
  • String Slicing: Subsets of strings can be accessed using slicing.
    • Example: name[0:3] returns 'Ali'.
  • String Methods:
    • lower(): Converts string to lowercase.
    • upper(): Converts string to uppercase.
    • len(): Returns the length of the string.

1.9 Basic Operators

Python supports several types of operators:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, >, <, >=, <=
  • Logical Operators: and, or, not
  • Assignment Operators: =, +=, -=, *=, /=

1.10 Understanding Coding Blocks

In Python, coding blocks are defined by indentation, not curly braces or keywords. Consistent indentation is crucial, as it determines the structure and flow of the program.

For example:

python

if a > b: print("a is greater than b") else: print("b is greater than or equal to a")

1.11 Defining List and List Slicing

A list in Python is a collection of items that can be of different types. Lists are mutable, meaning their content can be modified.

  • Creating a List: my_list = [1, 2, 3, 4]
  • List Indexing: Access elements using index, starting from 0.
    • Example: my_list[0] returns 1.
  • List Slicing: Get a subset of a list.
    • Example: my_list[1:3] returns [2, 3].

1.12 Other Data Types (Tuples, List, Dictionary in Python, Arrays, Associative Arrays)

  • Tuple: Immutable version of a list.

    • Example: my_tuple = (1, 2, 3)
  • Dictionary: A collection of key-value pairs.

    • Example: my_dict = {"name": "Alice", "age": 25}
  • Arrays: In Python, arrays can be implemented using lists, or the array module for more efficiency.

  • Associative Arrays (Dictionaries in Python): These are key-value stores that allow fast lookups by keys.


These foundational topics help students understand the basics of Python programming, including setting up the environment, working with different data types, and using essential operators for coding.

Post a Comment

0 Comments