Selected topic

Introduction to Python

Basics

Prefer practical output? Use related tools below while reading.

Variables

In Python, you can store values in variables. Variables are nothing but names given to a location in memory where a value can be stored.
  • Example: x = 5 (stores the integer value 5 in a variable named x)
  • Print the value using print(x) which will output: 5

Data Types

Python has several built-in data types:
  1. Integers (int): whole numbers, e.g., 1, 2, 3, etc.
* Example: x = 10
  1. Floats (float): decimal numbers, e.g., 3.14, -0.5, etc.
* Example: y = 3.14
  1. Strings (str): sequences of characters, e.g., "hello", 'goodbye', etc.
* Example: greeting = "Hello, World!" or greeting = 'Hello, World!'
  1. Boolean (bool): true or false values
* Example: x = True

Operators

Python has various operators for performing arithmetic, comparison, logical, and assignment operations:
  1. Arithmetic Operators:
* Addition: a + b * Subtraction: a - b Multiplication: a b * Division: a / b (returns a float) * Floor Division: a // b (returns an integer)
  1. Comparison Operators:
* Equal to: a == b * Not equal to: a != b * Greater than: a > b * Less than: a < b
  1. Logical Operators:
* AND: a and b * OR: a or b
  1. Assignment Operators:
* Assign: x = 5 * Add and assign: x += 5

Control Structures

Control structures determine the flow of a program's execution:
  1. Conditional statements (if-else):
* If statement: if x > 10: print("x is greater than 10")
  1. Loops:
* For loop: for i in range(5): print(i) * While loop: i = 0; while i < 5: print(i); i += 1

Functions

A function is a block of code that can be executed multiple times:
  1. Defining a function: def greet(name): print("Hello, " + name)
  2. Calling a function: greet("John")
These are the basic concepts in Python programming. This summary should give you a good starting point to learn more about the language!