Basics
The building blocks of every Python program — how to show output, name things, structure code, and get input from a user.
The print() function writes output to the screen. Pass it multiple values separated by commas and Python prints them with a space between each one by default.
print("ByteWise", "Ultimate")
Two optional keyword arguments give you more control: sep changes what's inserted between values, and end changes what's printed after the last one (a newline, "\n", by default).
print("ByteWise", "Ultimate", sep=" | ")
print("Loading", end="...")
print("done")
Keywords & Identifiers
Keywords are reserved words built into Python's grammar. You can't use them as variable names because they already have a special meaning. Some of the most common ones:
if,elif,else— conditional statementsfor,while— loopsdef,return— defining and exiting functionsTrue,False,None— the only built-in keywords that start with a capital letterand,or,not,in,is— logical, membership, and identity operatorsbreak,continue,pass— loop controltry,except,finally— exception handling
Identifiers are the names you choose for variables, functions, and everything else. A few rules keep them valid:
- Must start with a letter or an underscore — never a digit
- Can contain letters, digits, and underscores after that first character
- No spaces or other special characters allowed
- Can't be a keyword
- Case-sensitive —
ageandAgeare different variables
user_name = "ByteWise" # valid _score = 100 # valid, starts with an underscore 2nd_place = "silver" # invalid - starts with a digit class = "example" # invalid - 'class' is a keyword
Blocks & Indentation
Instead of curly braces, Python uses indentation to mark where a block of code starts and ends. The convention is 4 spaces per level. Every colon-terminated line — if, for, while, def, and more — opens a new indented block underneath it.
def square_all(n):
for i in range(n):
result = i ** 2
print(result)
square_all(4)
Mixing tabs and spaces, or indenting inconsistently, raises an IndentationError. Python is strict about this on purpose — indentation always reflects the real structure of your code.
Operators
Operators combine values into new ones. Python groups them into a few families.
Arithmetic — do the math you'd expect, with two small surprises: / always returns a float, even on an exact division, and // (floor division) returns a float too if either operand is already a float.
print(6 / 2) print(7 // 2) print(6.5 // 2) print(17 % 6)
Relational — compare two values and return a bool. Strings are compared character by character using their underlying character codes, not their length.
print(3 == 3)
print("a" > "z")
print("Bro" < "x")
Logical — combine boolean expressions. and needs both sides True, or needs at least one, and not flips the result.
a = 3 > 4 b = 0 != 1 print(a and b) print(a or b) print(not b)
Assignment — = assigns a value. The augmented forms (+=, -=, *=, /=, //=, %=, **=) update a variable using its current value in one step.
a = 16 a += 5 a -= 5 a *= 2 print(a)
Identity & Membership — is / is not check whether two names point to the exact same object in memory, a different question from whether they're equal (==). in / not in check whether a value exists inside a sequence.
s1 = "ByteWise"
s2 = "ByteWise"
print(s1 == s2)
print(s1 is s2)
print("Byte" in s1)
Comments
Comments are notes for humans — Python ignores them completely when running the code. A single-line comment starts with #. For notes spanning multiple lines, wrap them in triple quotes.
# This explains what the next line does total = price * quantity # inline comments work too """ This is a multi-line comment. Useful for longer explanations. """
Input
input() pauses the program and waits for the user to type something and press Enter. Whatever they type always comes back as a string — even if it looks like a number — so you need to convert it explicitly to do math with it.
name = input("Enter your name: ")
print(name, type(name))
Trying to add a raw input straight to a number raises a TypeError, because you'd be mixing a string with an int:
age = input("Enter age: ")
print(age + 10)
Wrapping the input in int() fixes it:
age = int(input("Enter age: "))
print(age + 10)