Learn Python Basics: Syntax, Variables, and Operators - Textnotes

Learn Python Basics: Syntax, Variables, and Operators


Master the fundamentals of Python programming including syntax, variables, data types, operators, input/output, and type conversion. Perfect for beginners to start coding confidently.

1. Introduction to Python

Python is a high-level, interpreted, and general-purpose programming language. It is known for its simplicity, readability, and large community support.

Key Features:

  1. Easy to learn and read
  2. Interpreted and dynamically typed
  3. Supports multiple paradigms: procedural, object-oriented, functional
  4. Extensive standard library and third-party packages

Example:


print("Welcome to Python!")

2. Installing Python & IDE Setup

  1. Installation: Download Python from python.org and install it.
  2. IDE Setup:
  3. VS Code: Lightweight, customizable, supports Python extension
  4. PyCharm: Professional IDE with advanced features for Python
  5. Jupyter Notebook: Best for data science, allows code execution with Markdown notes

Example: Check Python installation:


python --version

3. Python Syntax, Comments, and Indentation

  1. Python uses indentation to define blocks of code.
  2. Comments are written using # for single-line or """ """ for multi-line.

Example:


# This is a single-line comment

"""
This is a
multi-line comment
"""

if True:
print("Python uses indentation instead of braces")

4. Variables & Data Types

Variables store values in memory and do not require explicit declaration.

Common Data Types:

  1. int – Integer numbers
  2. float – Decimal numbers
  3. str – Text strings
  4. bool – True or False
  5. None – Represents no value

Example:


age = 25 # int
price = 99.99 # float
name = "Chinmaya" # str
is_active = True # bool
data = None # NoneType

print(age, price, name, is_active, data)

5. Basic Operators

Operators allow performing arithmetic, comparison, and logical operations.

Arithmetic Operators:


a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333
print(a % b) # 1
print(a ** b) # 1000
print(a // b) # 3

Comparison Operators:


print(a == b) # False
print(a != b) # True
print(a > b) # True
print(a < b) # False
print(a >= b) # True
print(a <= b) # False

6. Input/Output

Python allows interaction with users via input() and outputs with print().

Example:


name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}, you are {age} years old.")

7. Type Conversion

Convert one data type into another using built-in functions:

Example:


num_str = "10"
num_int = int(num_str) # Convert string to integer
num_float = float(num_str) # Convert string to float
num_str_again = str(123) # Convert number to string

print(num_int + 5) # 15
print(num_float + 2.5) # 12.5
print(num_str_again + "4") # "1234"