Python allows for interactive experimentation and scripting with ease.
Comments start with # and extend to the end of the line. They can appear at the beginning of aline or after code segment (inline comments), but not within string literals. Examples:
# This is a comment
spam = 1 # Inline comment
text = "# This is not a comment because it's inside quotes."Python's interpreter can function as a calculator. Arithmetic operations are straightforward, and parentheses can be used for grouping.
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # Division always returns a float
1.6- Numbers like
2,4,20are of typeint. - Numbers with fractional parts like
5.0,1.6are of typefloat.
/performs division and returns a float.//performs floor division and discards the fractional part.%calculates the remainder.
Examples:
>>> 17 / 3
5.666666666666667
>>> 17 // 3
5
>>> 17 % 3
2
# rebuilding 17
>>> 5 * 3 + 2 # floored quotient * divisor + remainder
17Use the ** operator to calculate powers:
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128The ** operator has higher precedence than -:
??? get the operator precedence
>>> -3**2
-9
>>> (-3)**2
9The = operator assigns values to variables. Variables can then be used in calculations.
>>> width = 20
>>> height = 5 * 9
>>> width * height
900Attempting to use an undefined variable results in an error, NameError:
>>> n
Traceback (most recent call last):
NameError: name 'n' is not definedPython converts integers to floating-point numbers when mixed with floats:
>>> 4 * 3.75 - 1
14.0The last printed result is stored in the _ variable, making it useful for continuing calculations:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06Important: Avoid explicitly assigning a value to _, assignment creates an independent local variable with the same name masking the built-in variable.
The Decimal class from the decimal module provides precise decimal arithmetic, which is useful for financial and scientific applications.
>>> from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.2') # Avoids floating-point precision errors
Decimal('0.3')
>>> Decimal('1.23456789').quantize(Decimal('0.01')) # Rounds to two decimal places
Decimal('1.23')Use the Fraction class from the fractions module for exact arithmetic with rational numbers.
>>> from fractions import Fraction
>>> Fraction(1, 3) + Fraction(2, 3)
Fraction(1, 1)Python supports complex numbers natively, with j or J indicating the imaginary part.
>>> 3 + 5j # Complex number
(3+5j)
>>> (2 + 3j) * (1 - 4j)
(14-5j)Python allows for manipulation of text, represented by the str type. This includes words, sentences, special characters, or even numerals enclosed in quotes.
Strings in Python can be enclosed in single quotes ('...') or double quotes ("...") with the same result.
There is no character type in Python, character is just string of length one.
# Single quotes
>>> 'spam eggs'
'spam eggs'
# Double quotes
>>> "Hello, World!"
'Hello, World!'
# Strings with numbers
>>> '1975'
'1975'Use the backslash (\) to escape quotes, or use alternating quotes for convenience.
>>> 'doesn\'t' # Escaping a single quote
"doesn't"
>>> "doesn't" # Using double quotes
"doesn't"
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Yes," they said.'
'"Yes," they said.'In the Python shell, the string definition and output string can look different. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), special characters are included in the string
'First line.\nSecond line.'
>>> print(s) # with print(), special characters are interpreted, so \n produces, new line
First line.
Second line.Raw strings, denoted by prefixing the string with r, treat backslashes (\) as literal characters. This prevents special characters like \n from being interpreted.
>>> print(r'C:\some\name')
C:\some\nameRaw strings cannot end with an odd number of backslashes because the final backslash escapes the closing quote.
# This will cause an error
>>> r"C:\some\path\"
SyntaxError: EOL while scanning string literal
# Workaround: Use normal strings with escaping
>>> "C:\\some\\path\\"
'C:\\some\\path\\'Use triple quotes ("""...""" or '''...''') for strings spanning multiple lines. End of lines are automatically included in the string, but it's possible to prevent this by adding a \ at the end of the line.
>>> print("""\
... Usage: command [OPTIONS]
... -h Display help
... -o FILE Output to FILE
... """)
Usage: command [OPTIONS]
-h Display help
-o FILE Output to FILE
>>> print('hello \
... boy')
hello boyStrings can be concatenated using + and repeated using *.
>>> 'un' + 'happy'
'unhappy'
>>> 'ha' * 3
'hahaha'Adjacent strings separated by a space are automatically concatenated. * Very useful when we want to break long strings. You can also use \ backslash.
>>> 'Py' 'thon' # Adjacent string literals are concatenated
'Python'
# have to enclose the string within paranthesis
# else text will just get the first quoted string. Think!
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
# An alternative way of doing the above.
>>> text = 'put \
... together'
>>> text
'put together'Note: This works only for literals. Use
+for variables or expressions.
>>> prefix = 'Py'
>>> prefix 'thon' # can't concatenate a variable and a string literal
File "<stdin>", line 1
prefix 'thon'
^^^^^^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^^^^^
SyntaxError: invalid syntax>>> prefix = 'Py'
>>> prefix + 'thon'
'Python'Strings are indexed starting at 0. Negative indices count from the end. In negative indexing, -0 is same as 0, hence they start from -1. Indexing allows you to get a character, slicing allows you to get a substring.
>>> word = 'Python'
>>> word[0] # First character
'P'
>>> word[-1] # Last character
'n'
>>> word[0:2] # Characters from index 0 (inclusive) to 2 (exclusive)
'Py'
>>> word[2:] # From index 2 to the end
'thon'
>>> word[:2] # From the start to index 2 (exclusive)
'Py'Attempting to use an index that is too large will result in an error:
>>> word[42] # the word only has 6 characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range-
Start is inclusive, and end is exclusive.
- It's done so that
s[:i] + s[i:] = s
- It's done so that
-
Tip for remembering slicing:
- One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0.
- Then the right edge of the last character of a string of n characters has index n, for example:
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
- The slice from i to j consists of all characters between the edges labeled i and j, repsectively.
-
Slices have useful defaults
- Omitted first index defaults to zero
- Ommited second index defaults to size of string being sliced.
-
For non-negative indices, length of a slice is the difference of indices, if both are within bounds.
-
Out-of-range slices are handled gracefully:
>>> word[4:42]
'on'
>>> word[42:]
''Strings in Python cannot be modified. To change a string, create a new one.
>>> word = "Python"
>>> word[0] = 'J' # Error
Traceback (most recent call last):
TypeError: 'str' object does not support item assignment
>>> 'J' + word[1:] # Create a new string
'Jython'Use len() to find the length of a string.
>>> len('supercalifragilisticexpialidocious')
34Strings are sequences of characters and support many common sequence operations. These include indexing, slicing, concatenation, and more.
word = "Python"
print(word[0]) # 'P' (first character)
print(word[-1]) # 'n' (last character)
print(word[0:3]) # 'Pyt' (substring from position 0 to 3, exclusive)greeting = "Hello, " + "World!"
print(greeting) # 'Hello, World!'
print("Hi! " * 3) # 'Hi! Hi! Hi!'print('H' in "Hello") # True
print('z' not in "Hello") # TruePython strings come with a rich set of methods for common tasks like case conversion, splitting, joining, and searching.
print("hello".upper()) # 'HELLO'
print("WORLD".lower()) # 'world'
print("python".capitalize()) # 'Python'
print("tItLe".title()) # 'Title'text = "hello world"
print(text.find("world")) # 6 (index where "world" starts)
print(text.replace("world", "Python")) # 'hello Python'data = "apple,orange,banana"
print(data.split(",")) # ['apple', 'orange', 'banana']
print(", ".join(['apple', 'orange', 'banana'])) # 'apple, orange, banana'Introduced in Python 3.6, f-strings allow you to embed expressions directly in string literals for easy and efficient formatting.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
# Output: 'My name is Alice and I am 25 years old.'print(f"The sum of 5 + 3 is {5 + 3}.")
# Output: 'The sum of 5 + 3 is 8.'pi = 3.14159
print(f"Pi rounded to 2 decimals: {pi:.2f}")
# Output: 'Pi rounded to 2 decimals: 3.14'
num = 1000
print(f"{num:2.2}")
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: Precision not allowed in integer format specifier
>>> The str.format() method provides a more powerful way to format strings.
print("Hello, {}!".format("World"))
# Output: 'Hello, World!'
print("Name: {name}, Age: {age}".format(name="Alice", age=25))
# Output: 'Name: Alice, Age: 25'print("Pi: {:.2f}".format(3.14159)) # Output: 'Pi: 3.14'
print("{:d}".format(42)) , # Output: '42'print("{:<10}".format("left")) # 'left '
print("{:>10}".format("right")) # ' right'
print("{:^10}".format("center")) # ' center 'Before str.format() and f-strings, Python used the % operator for string formatting.
print("Hello, %s!" % "World")
# Output: 'Hello, World!'print("Pi: %.2f" % 3.14159) # Output: 'Pi: 3.14'
print("%d items" % 42) # Output: '42 items'print("Name: %s, Age: %d" % ("Alice", 25))
# Output: 'Name: Alice, Age: 25'- The
str.format()method and f-strings are preferred for modern string formatting due to their flexibility and clarity. - The older
%formatting style is still functional but less versatile compared to newer methods.
Lists are sequential compound data types that group together values in a specific order. Lists are defined using square brackets [], with items separated by commas. They can contain items of different types, though typically, items are of the same type.
squares = [1, 4, 9, 16, 25]
print(squares) # Output: [1, 4, 9, 16, 25]Lists, like strings, support indexing and slicing.
# Indexing
print(squares[0]) # Output: 1
print(squares[-1]) # Output: 25
# Slicing
print(squares[-3:]) # Output: [9, 16, 25]Repetition operator in Python is very efficient.
print(squares + [36, 49, 64, 81, 100])
# Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print([1]*5)
# Output: [1, 1, 1, 1, 1]Unlike strings, lists are mutable, meaning their content can be changed.
We can add new items to list using append() method.
# Modifying a single element
cubes = [1, 8, 27, 65, 125]
cubes[3] = 64 # Fixing the incorrect value
print(cubes) # Output: [1, 8, 27, 64, 125]
# Adding new elements
cubes.append(216) # Adding 6³
cubes.append(7 ** 3) # Adding 7³
print(cubes) # Output: [1, 8, 27, 64, 125, 216, 343]Assigning a list to a new variable creates a reference, not a copy. Hence, changes made to one variable affect the other.
id() gives the memory location referred by the variable.
rgb = ["Red", "Green", "Blue"]
rgba = rgb
>>> id(rgb) == id(rgba) # they reference the same object
True
rgba.append("Alpha")
print(rgb) # Output: ["Red", "Green", "Blue", "Alpha"]To create a copy, use slicing or .copy() method:
# Using slicing
correct_rgba = rgba[:]
correct_rgba[-1] = "Alpha"
print(correct_rgba) # Output: ["Red", "Green", "Blue", "Alpha"]
print(rgba) # Output: ["Red", "Green", "Blue", "Alpha"]
# .copy() method
correct_rgba = rgba.copy()
correct_rgba[-1] = "Alpha"
print(correct_rgba) # Output: ["Red", "Green", "Blue", "Alpha"]
print(rgba) # Output: ["Red", "Green", "Blue", "Alpha"]Lists allow replacing, removing, or clearing elements using slice assignments.
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Replacing values
letters[2:5] = ['C', 'D', 'E']
print(letters) # Output: ['a', 'b', 'C', 'D', 'E', 'f', 'g']
# Removing values
letters[2:5] = []
print(letters) # Output: ['a', 'b', 'f', 'g']
# Clearing the list
letters[:] = []
print(letters) # Output: []Returns the number of items in a list.
letters = ['a', 'b', 'c', 'd']
print(len(letters)) # Output: 4Lists can contain other lists, creating a nested structure.
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
print(x) # Output: [['a', 'b', 'c'], [1, 2, 3]]
print(x[0]) # Output: ['a', 'b', 'c']
print(x[0][1]) # Output: 'b'>>> n
Traceback (most recent call last):
NameError: name 'n' is not defined>>> prefix = 'Py'
>>> prefix 'thon' # can't concatenate a variable and a string literal
File "<stdin>", line 1
prefix 'thon'
^^^^^^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^^^^^
SyntaxError: invalid syntax>>> word[42] # the word only has 6 characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range>>> word[0] = 'J' # Error
Traceback (most recent call last):
TypeError: 'str' object does not support item assignmentThe Fibonacci sequence can be generated using the following code:
# Fibonacci series:
# The sum of two elements defines the next
a, b = 0, 1
while a < 10:
print(a, sep=", ")
a, b = b, a + b
# Outputs: 0, 1, 1, 2, 3, 5, 8Python allows simultaneous assignment of multiple variables. During assignment, expressions on the right-hand side are evaluated left to right before the values are assigned:
a, b = 0, 1The while loop runs as long as the condition evaluates to True.
Conditions can include:
- Numbers: Non-zero integers are
True; zero isFalse. - Sequences (strings, lists, etc.): Non-empty sequences are
True; empty sequences areFalse.
- Python uses indentation to group statements.
- Every line in the block must be indented by the same amount.
- At the interactive prompt:
- Type spaces or tabs for indentation.
- Follow compound statements with a blank line to indicate completion.
The print() function writes output to the screen. Differences from writing expressions in interactive terminal:
- Handles multiple arguments, floating-point numbers, and strings differently.
- Automatically inserts spaces between arguments.
- Prints strings without quotes.
- Can customize Output with
endkeyword argument- Avoid the default newline after output.
- Specify a different ending string.
<(less than),>(greater than)==(equal to),!=(not equal to)<=(less than or equal to),>=(greater than or equal to)