Informal Intro Tutorial
TL;DR
Python’s interactive interpreter lets you experiment immediately with numbers, strings, and lists using intuitive syntax and meaningful indentation.
Interesting!
Python uses indentation instead of braces to define code blocks - making the visual structure of your code match its logical structure!
Interactive Python Interpreter
python code snippet start
# Start Python and see the >>> prompt
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # Division always returns a float
1.6
# Variables and assignment
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
python code snippet end
Working with Numbers
python code snippet start
# Different number types
>>> 17 / 3 # Classic division returns float
5.666666666666667
>>> 17 // 3 # Floor division
5
>>> 17 % 3 # Remainder
2
>>> 5 ** 2 # Power
25
# Mixed operations
>>> 4 * 3.75 - 1
14.0
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _ # _ holds the last printed value
113.0625
python code snippet end
String Basics
python code snippet start
# Single or double quotes
>>> 'spam eggs'
'spam eggs'
>>> "spam eggs"
'spam eggs'
>>> 'doesn\'t' # Escape quote with backslash
"doesn't"
>>> "doesn't" # Or use double quotes
"doesn't"
# Multi-line strings
>>> print("""\
... Usage: thingy [OPTIONS]
... -h Display this usage message
... -H hostname Hostname to connect to
... """)
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
python code snippet end
String Operations
python code snippet start
# Concatenation and repetition
>>> 'Py' + 'thon'
'Python'
>>> 3 * 'un' + 'ium'
'unununium'
# String indexing
>>> word = 'Python'
>>> word[0] # First character
'P'
>>> word[5] # Last character
'n'
>>> word[-1] # Last character (negative indexing)
'n'
>>> word[-2] # Second to last
'o'
# String slicing
>>> word[0:2] # Characters from 0 to 2 (exclusive)
'Py'
>>> word[2:5] # Characters from 2 to 5
'tho'
>>> word[:2] # Beginning to 2
'Py'
>>> word[4:] # From 4 to end
'on'
>>> word[-2:] # Last two characters
'on'
python code snippet end
Lists Fundamentals
python code snippet start
# Creating and accessing lists
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0] # Indexing
1
>>> squares[-1] # Last item
25
>>> squares[-3:] # Last three items
[9, 16, 25]
# Lists are mutable
>>> cubes = [1, 8, 27, 65, 125] # Oops, 4³ is 64, not 65!
>>> cubes[3] = 64 # Fix the error
>>> cubes
[1, 8, 27, 64, 125]
python code snippet end
List Operations
python code snippet start
# Adding elements
>>> cubes.append(216) # Add 6³
>>> cubes.append(7 ** 3) # Add 7³
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
# List slicing and assignment
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E'] # Replace some values
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = [] # Remove them
>>> letters
['a', 'b', 'f', 'g']
# Length and nesting
>>> len(letters)
4
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n] # Nested list
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
python code snippet end
First Programming Example
python code snippet start
# Fibonacci series up to n
>>> a, b = 0, 1 # Multiple assignment
>>> while a < 10:
... print(a)
... a, b = b, a+b # Multiple assignment
...
0
1
1
2
3
5
8
# The loop body is indented!
# Indentation groups statements together
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536
python code snippet end
Interactive Features
python code snippet start
# Comments start with #
>>> # This is a comment
... 2 + 2 # And a comment on the same line
4
# Using variables
>>> message = "Hello, World!"
>>> print(message)
Hello, World!
# Everything is an object
>>> name = "Python"
>>> print(f"Hello, {name}!") # f-string (modern way)
Hello, Python!
# Length of strings and lists
>>> len("Hello")
5
>>> len([1, 2, 3, 4, 5])
5
python code snippet end
Control Flow Basics
python code snippet start
# Simple conditionals (more detail in later tutorials)
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
# For loops with lists
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
python code snippet end
Built-in Functions
python code snippet start
# Common functions you'll use often
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(5, 10))
[5, 6, 7, 8, 9]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> sum([1, 2, 3, 4, 5])
15
>>> max([1, 2, 3, 4, 5])
5
>>> min([1, 2, 3, 4, 5])
1
# Type checking
>>> type(42)
<class 'int'>
>>> type("hello")
<class 'str'>
>>> type([1, 2, 3])
<class 'list'>
python code snippet end
Python’s interactive nature makes it perfect for learning and experimentation - you can try ideas immediately and see results without writing complete programs!
Start your Python journey with understanding Python's philosophy before diving into these interactive features. From these basics, you can explore control flow tools and data structures for more powerful programming techniques. The interactive interpreter features shown here complement interpreter usage patterns and the built-in functions you’ll use daily. Advanced text processing with regular expressions and data persistence using pickle serialization build naturally on these fundamentals.
Reference: An Informal Introduction to Python