You're offline — cached pages and worlds still work
Drishti Innovations logo
Drishti Innovations

Python Syntax & Variables

Foundational programming syntax, identifiers, variables, data types. Aligned with neural network training simulator.

Python Syntax & Variables

Python Syntax & Variables

What you'll learn

  • Python's basic syntax rules and how to write clean code.
  • How to declare and use variables and understand data types.
  • Input/output with print() and input().
  • Type conversion and basic operators.

Key concepts

Python Basics

  • Python is interpreted — runs line by line; no compilation needed.
  • Indentation is syntax: blocks are defined by spaces (not {}).
  • Case-sensitive: Namename.
  • Comments: # single line or """docstring""".

Variables

name = "Alice"        # str
age = 17              # int
gpa = 9.5             # float
is_student = True     # bool
  • No type declaration needed (dynamic typing).
  • Variable names: letters, digits, underscores; cannot start with digit.
  • Convention: snake_case for variables and functions.

Data Types

TypeExampleNotes
int42, -7Whole numbers
float3.14, -0.5Decimal numbers
str"hello", 'world'Text; immutable
boolTrue, FalseCapitalized in Python
NoneTypeNoneRepresents no value

Type Conversion

int("42")       # → 42
float("3.14")   # → 3.14
str(100)        # → "100"
bool(0)         # → False  (0, "", None, [] are falsy)

Input / Output

name = input("Enter your name: ")   # always returns str
print("Hello,", name)
print(f"Hello, {name}!")            # f-string (preferred)

Arithmetic Operators

OperatorMeaningExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication3 * 412
/Division (float)7 / 23.5
//Floor division7 // 23
%Modulus (remainder)7 % 21
**Exponentiation2 ** 38

Comparison & Logical Operators

x == y    # Equal
x != y    # Not equal
x > y     # Greater than
x >= y    # Greater than or equal

# Logical
x and y
x or y
not x

String Operations

s = "Python"
len(s)          # 6
s[0]            # 'P'  (indexing)
s[-1]           # 'n'  (last char)
s[1:4]          # 'yth' (slicing)
s.upper()       # 'PYTHON'
s.lower()       # 'python'
s + " 3"        # 'Python 3' (concatenation)
"Py" in s       # True

Multiple Assignment

a, b, c = 1, 2, 3
x = y = z = 0
a, b = b, a     # swap

Quick check

  • What is the difference between / and // in Python?
  • Write code to take two numbers as input and print their sum.
  • What is the output of bool("") and bool(1)?
  • What is an f-string? Show an example.
  • What does 7 % 3 evaluate to?

Open the Practice tab for graded questions on Python Syntax & Variables.

Key Takeaways (TL;DR)

  • What you'll learn
  • Key concepts
  • Quick check

Master this topic with Drishti OS

Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.

Start Free Practice