Brief Overview of Python

This chapter provides a brief overview of Python, covering programming basics, including keywords, identifiers, variables, data types, operators, expressions, input/output, functions, and control statements such as loops and conditionals.

3.1 Introduction to Python

Python is a high-level, open-source programming language that was created by Guido van Rossum in 1991. It is popular due to its simplicity and versatility, making it applicable for various domains such as software development, data science, artificial intelligence, and scientific computing. Python code is typically written in plain text files with a .py extension and can be run through an interpreter, which can be installed locally or accessed online.

Working with Python

To start coding in Python, you can either use an interactive mode or script mode:

  • Interactive Mode: Enter commands directly into the Python shell (interpreter). This mode is quick for testing small pieces of code but doesn’t allow saving your work.
  • Script Mode: Write and save code in a script file, which is executed all at once by the interpreter (using the command python filename.py). This is the preferred method for more complex programs, as scripts can be easily modified and reused.

3.2 Python Keywords

Keywords in Python are reserved words that have specific meanings to the interpreter. They must be written in lowercase and cannot be used as identifiers for variables, functions, etc. Examples include def, if, else, return, and more.

List of Keywords:

  1. False, None, True
  2. and, as, assert, async, await
  3. break, class, continue, def, del
  4. elif, else, except, finally, for
  5. from, global, if, import, in, is
  6. lambda, nonlocal, not, or, pass
  7. raise, return, try, while, with, yield

3.3 Identifiers

Identifiers are names given to variables, functions, and other objects. Rules for naming identifiers include:

  • Must begin with a letter (A-Z, a-z) or an underscore (_).
  • Can contain letters, digits (0-9), and underscores afterward.
  • Cannot begin with digits or contain special characters (e.g., @, #, $, etc.).
  • Cannot be a keyword in Python.

Example: marks_math, marks_english, avg.

3.4 Variables

Variables are identifiers whose values can vary. They must be assigned a value before use. An example of variable assignment is:

age = 18

Variables can hold different data types: strings, integers, floats, etc.

3.5 Data Types

Python supports several built-in data types to categorize the type of data variables can hold:

  • Numeric Types: integers (int), floating-point numbers (float), and complex numbers (complex).
  • Sequence Types: Strings, Lists and Tuples.
  • Mapping Type: A dictionary (dict) holds key-value pairs.
  • Boolean: Holds True or False values.

Example:

num = 5 #int
price = 19.99 #float
student_names = ['John', 'Alice', 'Bob'] #list
data = {'color': 'blue', 'age': 25} #dictionary

3.6 Operators

Operators in Python are symbols used to perform operations on variables and values. They include:

  • Arithmetic Operators: +, -, *, /, %, // (floor division), and ** (exponentiation).
  • Relational Operators: ==, !=, >, <, >=, <=.
  • Logical Operators: and, or, not.
  • Assignment Operators: =, +=, -=, *=, etc.

3.7 Expressions

An expression is a combination of constants and variables that is evaluated to produce a value. Examples include:

x + y 
3 * (8 + 2)

Furthermore, operator precedence affects how expressions are evaluated; for example, multiplication is performed before addition unless parentheses are used.

3.8 Input and Output

  • Input: To receive user input, the function input() is used.
  • Output: The function print() displays values on the screen. Example usage:
user_name = input('Enter your name: ')
print('Hello,', user_name)

3.9 Debugging

Debugging is the process of identifying and removing errors from a program’s code. Errors can be:

  1. Syntax Errors: Errors in the code structure or grammar.
  2. Logical Errors: The code runs but produces incorrect results.
  3. Runtime Errors: Issues that stop normal execution such as division by zero.

3.10 Functions

A function is a reusable piece of code that executes a specific task. Example:

def greet(name):
    print(

Key terms/Concepts

  • Python is an open-source, high-level interpreted programming language created by Guido van Rossum in 1991.
  • Keywords in Python are reserved and must follow specific syntax rules.
  • An Identifier must begin with a letter or underscore and can't be a keyword.
  • A Variable is a named store that holds data that can change.
  • Data Types include int, float, string, bool, and structures like list and dict.
  • Operators perform operations on values and can be arithmetic, relational, logical, or assignment.
  • An Expression combines values and operators to produce a result.
  • Input is taken using input(), while Output is displayed using print().
  • Debugging is the process of identifying and fixing errors in code, which can be syntax, logical, or runtime errors.
  • Functions are reusable pieces of code that perform specific tasks.

Other Recommended Chapters