BackPython Data Types, Control Structures, and Operators: Study Notes
Study Guide - Smart Notes
Tailored notes based on your materials, expanded with key definitions, examples, and context.
Coding Fundamentals in Python
Data Types
Python supports a variety of data types that allow programmers to store and manipulate different kinds of information. Understanding these types is essential for effective coding.
Numbers: Includes integers (int), floating-point numbers (float), and complex numbers (complex).
Strings: Sequences of Unicode characters, used to represent text.
Lists: Ordered, mutable collections of items, which can be of mixed types.
Tuples: Ordered, immutable collections of items.
Files: Objects that allow reading from and writing to external files.
Sets: Unordered collections of unique items.
Dictionaries: Unordered collections of key-value pairs.
Example: my_list = [1, "apple", 3.14]
Structures
Python programs are organized using structures that control the flow of execution. The three main types are:
Sequence: Instructions are executed one after another in order.
Decision: Execution depends on conditions (e.g., if statements).
Repetition: Instructions are repeated as long as a condition holds (e.g., while and for loops).
Example: if x > 0: print("Positive")
Program Structure in Python
Sequence Structure
In a sequence structure, statements are executed in the order they appear. This is the default mode of execution in Python.
Input is received.
Processing occurs.
Output is produced.
Decision Structure
Decision structures allow the program to choose between different paths based on conditions.
Uses if, elif, and else statements.
Evaluates a condition to determine which block of code to execute.
Example: if score >= 60: print("Pass") else: print("Fail")
Repetition Structure
Repetition structures (loops) repeat a block of code as long as a condition is true.
while loops: Repeat while a condition is true.
for loops: Iterate over a sequence (e.g., list, tuple).
Example: for i in range(5): print(i)
Structures that Control Flow
Control flow structures determine the order in which instructions are executed. They include sequence, decision, and repetition structures, and are essential for creating dynamic and responsive programs.
Relational and Logical Operators
Relational Operators
Relational operators compare values and return a Boolean result (True or False). They are used in conditions for decision and repetition structures.
==: Equal to
!=: Not equal to
<: Less than
>: Greater than
<=: Less than or equal to
>=: Greater than or equal to
in: Checks if an element is in a sequence
not in: Checks if an element is not in a sequence
Relational operators can be applied to numbers, strings, and other objects. For strings, comparison is based on ASCII values.
ASCII Values
ASCII (American Standard Code for Information Interchange) assigns numeric values to characters, which determines their order in string comparisons.
ASCII values range from 32 (space) to 126 (tilde ~) for standard printable characters.
Uppercase letters have lower ASCII values than lowercase letters.
Digits (0-9) have lower ASCII values than letters.
Character | ASCII Value |
|---|---|
Space | 32 |
0 | 48 |
A | 65 |
a | 97 |
z | 122 |
{ | 123 |
| | 124 |
} | 125 |
~ | 126 |
Example: ord('A') = 65
Lexicographical Order
Strings are compared in lexicographical order, similar to dictionary order.
All uppercase letters come before lowercase letters.
Spaces come before all other printable characters.
Digits come before all letters.
Example: "Apple" < "apple" evaluates to True because 'A' (65) < 'a' (97).
Logical Operators
Logical operators combine multiple relational expressions to form compound conditions.
and: True if both operands are True.
or: True if at least one operand is True.
not: Inverts the Boolean value.
Example: (x > 0) and (y < 10)
Relational Operators: Table
Python Notation | Numeric Meaning | String Meaning |
|---|---|---|
== | equal to | identical to |
!= | not equal to | different from |
< | less than | precedes lexicographically |
> | greater than | follows lexicographically |
<= | less than or equal to | precedes lexicographically or is identical |
>= | greater than or equal to | follows lexicographically or is identical |
in | substring of | substring of |
not in | not a substring of | not a substring of |
Examples of Relational Operators
Evaluating Conditions
Consider the following examples. Determine if each condition is True or False:
1 <= 1 → True
1 < 1 → False
"car" < "cat" → True (because 'r' < 't')
"Dog" < "dog" → True (because 'D' < 'd')
"fun" in "refunded" → True (substring check)
Additional info:
Relational operators can be used with lists and tuples, comparing elements in order until a difference is found.
Logical operators are essential for constructing complex decision-making logic in programs.
ASCII values can be accessed in Python using the ord() function.
Short-circuit evaluation means that Python stops evaluating a logical expression as soon as the result is determined.
Boolean data type (bool) has two values: True and False.