Creating neat Python basics notes for the requested topics.
🐍
Python Basics Notes
1. print() Function
The print() function is used to display output to the console.
● Syntax: print(value1, value2, ..., sep=' ', end='\n', file=[Link], flush=False)
● The most common usage is: print("Hello, World!")
● It can print variables, strings, and expressions.
● sep (separator): Specifies how to separate the arguments (default is a space).
○ Example: print('a', 'b', 'c', sep='-') outputs a-b-c
● end: Specifies what to print after the last argument (default is a newline character \n).
○ Example: print('Hello', end=' '); print('Python') outputs Hello Python
2. input() Function
The input() function is used to get user input from the console.
● Syntax: input([prompt])
● The optional prompt is a string displayed to the user before they enter data.
● Crucially, the data returned by input() is always a string (\text{str}), even if the user
enters a number.
○ Example: name = input("Enter your name: ")
3. Lists
Lists are ordered, mutable (changeable) sequences of items. They can hold items of different
data types.
● Creation: Defined by enclosing comma-separated values in square brackets [].
○ Example: my_list = [1, 'apple', 3.14, True]
● Indexing: Items are accessed using zero-based indices (0 for the first item). Negative
indices count from the end (-1 for the last item).
○ Example: my_list[0] is 1; my_list[-1] is True
● Common Methods:
○ append(item): Adds an item to the end.
○ insert(index, item): Inserts an item at a specific position.
○ remove(item): Removes the first occurrence of a value.
○ pop([index]): Removes and returns the item at a given index (default is the last
item).
○ len(list): Returns the number of items.
4. Tuples
Tuples are ordered, immutable (unchangeable) sequences of items. Like lists, they can hold
different data types.
● Creation: Defined by enclosing comma-separated values in parentheses () (parentheses
are often optional).
○ Example: my_tuple = (10, 'banana', 2.5)
● Immutability: Once created, you cannot add, remove, or change elements in a tuple.
This makes them useful for data that shouldn't be modified (e.g., coordinates, database
records).
● Indexing: Items are accessed using zero-based indices, just like lists.
○ Example: my_tuple[1] is 'banana'
● When to use: Use tuples for collections of items that should not change.
5. Data Types and Type Casting
Data Types
Every value in Python has a data type. Essential built-in types include:
Type Description Example
int Integers (whole numbers, 10, -5
positive or negative)
float Floating-point numbers 3.14, -0.5
(numbers with a decimal point)
str Strings (sequences of 'hello', "Python"
characters)
bool Boolean values (Truth values) True, False
list Ordered, mutable sequence [1, 2, 3]
tuple Ordered, immutable sequence (1, 2, 3)
● type() function: Use type(variable) to determine the data type of a variable.
Type Casting (Type Conversion)
Type casting is the process of converting one data type into another. This is done using the
type's constructor function.
● int(x): Converts x to an integer (truncates the decimal part for floats).
○ Example: int('123') is 123; int(5.9) is 5
● float(x): Converts x to a float.
○ Example: float(10) is 10.0
● str(x): Converts x to a string.
○ Example: str(123) is '123'
● list(x): Converts a sequence x to a list.
● tuple(x): Converts a sequence x to a tuple.
6. Simple Mathematical Operators
Python supports standard arithmetic operations:
Operator Name Example Result
+ Addition 5+2 7
- Subtraction 5-2 3
* Multiplication 5*2 10
/ Division (always returns 5 / 2 2.5
a float)
Operator Name Example Result
// Floor Division (returns 5 // 2 2
integer quotient)
% Modulus (returns the 5 % 2 1
remainder)
** Exponentiation 5 ** 2 25
● Operator Precedence: Follows the standard mathematical order (PEMDAS/BODMAS):
Parentheses, Exponents, Multiplication/Division/Modulus, Addition/Subtraction.
7. Loops
Loops are used to repeat a block of code multiple times.
a. for Loop
The for loop is used for iterating over a sequence (like a list, tuple, or string) or other iterable
objects.
● Syntax:
for item in sequence:
# code to execute for each item
● Using range(): The range() function generates a sequence of numbers, often used to
loop a specific number of times.
○ range(stop): 0 up to (but not including) stop.
○ range(start, stop): start up to (not including) stop.
○ range(start, stop, step): start up to (not including) stop, incrementing by step.
<!-- end list --># Example: Prints 0, 1, 2
for i in range(3):
print(i)
b. while Loop
The while loop executes a set of statements as long as a condition is true.
● Syntax:
count = 0
while condition:
# code to execute
# must include code to eventually make the condition False
count += 1
Loop Control Statements
● break: Terminates the entire loop immediately.
● continue: Skips the current iteration of the loop and moves to the next one.
8. Conditional Statements
Conditional statements are used to execute code only if certain conditions are met.
a. if Statement
The if statement executes code if its condition is True.
● Syntax:
if condition_is_true:
# code to run if condition is True
b. if...else Statement
Executes one block of code if the condition is True and a different block if it is False.
● Syntax:
if condition:
# code if condition is True
else:
# code if condition is False
c. if...elif...else Statement
Allows checking multiple conditions sequentially. elif stands for "else if".
● Syntax:
if condition1:
# code if condition1 is True
elif condition2:
# code if condition1 is False and condition2 is True
else:
# code if all conditions above are False
Comparison and Logical Operators
Operator Name Example
== Equal to a == b
!= Not equal to a != b
> Greater than a>b
< Less than a<b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
and Logical AND x > 5 and x < 10
or Logical OR x < 5 or x > 10
not Logical NOT not (x > 5)