Values, variables, functions, methods
This module includes:
1. Values
Python has several basic types of values. The most common include:
- Strings (text)
- Integers (whole numbers)
- Floats (decimal numbers)
- Booleans (
True
andFalse
) - None (represents “no value”)
▷ Strings
Strings are sequences of characters interpreted as text. They are enclosed in single or double quotes:
s1 = "Hello, world!"
s2 = 'Rock & Roll'
-
Arithmetic with strings?
"9" + "3" # → "93" "9" + 3 # TypeError: can’t add str and int
-
Common string operations
# Concatenation "Hello, " + "Python!" # → "Hello, Python!" # Length len("NLTK") # → 4 # Indexing s = "Python" s[0] # → "P" s[-1] # → "n"
Integers
Integers are whole numbers without a decimal point:
i1 = 1
i2 = 42
i3 = -7
-
Basic arithmetic
i1 + i2 # → 43 i2 - 10 # → 32 i3 * 3 # → -21 # Division in Python 3 7 / 2 # → 3.5 # true division 7 // 2 # → 3 # floor division
▷ Floats
Floats are numbers with decimal places:
f1 = 1.234
f2 = 2.0
f3 = -6.789
-
Mixing ints and floats
3 + 0.5 # → 3.5 7 / 2 # → 3.5
▷ Booleans
Booleans represent truth values: True
or False
.
is_hungry = True
is_sleepy = False
- Used in comparison
5 > 3 # → True
2 == 4 # → False
- More about comparison operators
Operator | Meaning | Example | Result |
---|---|---|---|
== |
Equal to | 2 == 2 |
True |
!= |
Not equal to | 3 != 4 |
True |
< , > , <= , >= |
Less than, greater than, etc. | 5 >= 2 |
True |
Example with !=
:
if [] != [None]:
print("These are different")
# Output: These are different
- Boolean logical operators:
Operator | Description | Example | Result |
---|---|---|---|
and |
True if both are True | True and False |
False |
or |
True if at least one is True | True or False |
True |
not |
Reverses the Boolean value | not True |
False |
x = True
y = False
x and y # → False
x or y # → True
not x # → False
▷ None
None
is a special value that means “no value” or “nothing.” It’s often used as a default or placeholder.
result = None
print(result) # → None
2. Variables
Variables store values of any type. Assignment is straightforward:
a = "this is a string"
b = 9
c = 3.2
print(b + c) # → 12.2
-
Reassignment
a = a + "!" print(a) # → "this is a string!"
3. Functions
Functions perform operations on values or objects. Common ones include:
Function | Description | Example |
---|---|---|
print(x) |
Display x in the console |
print("Hi!") → Hi! |
len(x) |
Return length of a string or list | len("abc") → 3 |
str(x) |
Convert x to a string |
str(16) → "16" |
int(x) |
Convert to integer (floats rounded down) | int(3.9) → 3 |
float(x) |
Convert to float | float("2.5") → 2.5 |
4. Methods
Methods are functions bound to objects (typically defined inside a class). For strings:
sample = "This is a STRING"
-
.lower()
sample.lower() # → "this is a string" # str.split(sep) – Splits a string into a list of substrings, using sep (separator) as the delimiter.
-
.split(sep)
sample.lower().split(" ") # → ['this', 'is', 'a', 'string'] # If sep is given → the string is split wherever that exact substring occurs. # If sep is omitted or set to None → the string is split on whitespace (spaces, tabs, newlines).
-
Splitting on another character
"banana".split("a") # → ['b', 'n', 'n', '']
-
Combining functions & methods
Chain calls for concise code:
# Lowercase, split into words, and count them
len("This is a STRING".lower().split()) # → 4
5. Exercises
-
Assign
# Assign a sentence that is relevant to you, for example, I wrote, string_sent = "I like running trails and drinking cold brew."
-
Count characters
print(len(string_sent))
-
Split into words
list_sent = string_sent.split(" ") print(list_sent)
-
Count words
print(len(list_sent))
-
Average characters per word
av_chars = len(string_sent) / len(list_sent)
-
Print as string
print(str(av_chars))