Skip to content

Python Basics

  • print(): Function to print the output to the console.
  • type(): Function to print the type of the variable
  • exit(): Function to exit the program

Comments

  • #: Used for single-line comments.
  • Ctrl + /: Shortcut to comment selected lines.

Data Types

1. Text Data Type

  • String: A collection of characters represented using single quotes or double quotes.

2. Numeric Data Type

  • Integer: Numbers without decimal points.
  • Float: Numbers with decimal points.
  • Complex: Numbers in the format a + bj, where a is the real part and b is the imaginary part.

3. Sequential Data Type

  • List:
  • Collection of elements (can be called an array).
  • Ordered collection.
  • Can contain any data type.
  • Mutable (changeable).
  • Indexed (first index position is 0).
  • Defined by [].

  • Tuple:

  • Collection of elements similar to a list.
  • Ordered collection.
  • Indexed (first index position is 0).
  • Immutable (not changeable).
  • Defined by ().

4. Mapping Data Type

  • Dictionary:
  • Collection of key-value pairs.
  • Not indexed.
  • Duplication of keys is not allowed.
  • Mutable.
  • Defined by {}.

5. Set Data Type

  • Set:
  • Collection of unique elements.
  • Not indexed.
  • Duplication of elements is not allowed.
  • Unordered.
  • Defined by {}.

6. Boolean Data Type

  • Boolean: Represents True or False.

Variables

A Python variable is a reserved memory location to store values. In other words, variables in a Python program give data to the computer for processing.

Rules for Python Variables

  1. Variables start with a letter or underscore.
  2. Cannot start with a number.
  3. Can only contain alphanumeric characters and underscores (_).
  4. Case-sensitive (e.g., AGE, age, Age are three different variable names).
  5. Keywords cannot be used as variables (e.g., and, or, if, def).

Reference Code