Notes Programming Updated
Notes Programming Updated
1. Variables: In Python, you can store values in variables using the assignment operator "=". For
example, x = 5 assigns the value 5 to the variable x.
2. Data types: Python supports different data types such as integers, floats, strings, and Booleans.
You can use functions such as type() to check the data type of a variable.
3. Operators: Python supports different operators such as arithmetic operators (+, -, *, /),
comparison operators (>, <, ==, !=).
4. Lists: Lists are used to store multiple items in a single variable. They can be modified using
different methods such as append(), remove(), and sort(). For example, my_list = [1, 2, 3]
5. Dictionaries: Dictionaries are used to store key-value pairs. They can be modified using different
methods such as update(), pop(), and clear(). For example, my_dict = {'name': 'John', 'age': 30}
6. Modules: Modules in Python are files containing Python code. They can be imported into other
Python scripts to reuse code. For example, import math imports the math module.
WHAT IS FSTRING
f-strings (or formatted string literals) are a way to embed expressions inside string literals, using a
syntax that starts with the letter "f". F-strings were introduced in Python 3.6 as a more concise and
readable alternative to traditional string formatting techniques.
Example:
name = "John"
age = 30
message = f"My name is {name} and I am {age} years old."
print(message)
UNDERSCORE
1. As a single variable name, '_' is a valid variable name that can be used to store a value. However,
it is commonly used as a placeholder variable for values that are not going to be used later in the
program.
2. As a prefix, '_' is often used to indicate that a method or attribute is intended to be used
internally by a module or class and is not part of the public API.
3. In the interactive interpreter, the last expression result is stored in the special variable '_' for
easy access. This is commonly used when experimenting with code in the interpreter.
4. Double underscore prefix (e.g., '__variable_name') is used for name mangling in Python classes.
This is a technique to make an instance variable private to the class.
IMPORT MATH
We import the math module in Python to gain access to mathematical functions and constants that
are not built into the Python language. The math module provides a wide range of mathematical
operations that can be used in your Python programs.
Here are some examples of what you can do with the math module:
Compute square roots, logarithms, and trigonometric functions such as sine, cosine, and
tangent.
import math
x = math.sqrt(16)
y = math.sin(math.pi/2)
z = math.ceil(3.7)
w = math.radians(45)
OPERATORS
Python has several arithmetic operators for performing basic arithmetic operations. Here are the
most commonly used arithmetic operators in Python:
5. Floor Division (//): Divides one number by another and rounds down to the nearest integer.
Example: 15 // 4 = 3
6. Modulo (%): Divides one number by another and returns the remainder.
Example: 15 % 4 = 3
These operators can be used with both integers and floats. Additionally, they can be combined in
complex expressions using parentheses to group sub-expressions.
FUNCTIONS
In Python, there are two main modules that provide functions for mathematical operations: the
random module and the math module. Here's an explanation of each module along with some
examples:
Example:
randrange(stop): This function returns a randomly selected integer from the range 0 through
stop-1.
Example:
import random
print(random.randrange(10))
randrange(start, stop[, step]): This function returns a randomly selected integer from the range
start through stop-1, incremented by step.
Example:
import random
print(random.randrange(0, 10, 2))
Example:
import random
print(random.randint(1, 10))
choice(seq): This function returns a randomly selected element from the given sequence seq.
Example:
import random
colors = ['red', 'green', 'blue']
print(random.choice(colors))
shuffle(seq): This function randomly shuffles the elements of the given sequence seq in place.
Example:
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
sample(population, k): This function returns a randomly selected sample of length k from the
given sequence population without replacement.
Example:
import random
numbers = [1, 2, 3, 4, 5]
sample = random.sample(numbers, 3)
print(sample)
MATH
Here are some of the commonly used math functions provided by the Python math module:
log(x[, base]): Returns the logarithm of x with the given base. If base is not specified, it defaults
to e.
sin(x), cos(x), tan(x): Returns the trigonometric sine, cosine, and tangent of x, respectively.
asin(x), acos(x), atan(x): Returns the inverse trigonometric sine, cosine, and tangent of x,
respectively.
tau: A constant that represents the value of tau, which is twice the value of pi.
Here are all the functions available in the Python random module:
randrange([start], stop[, step]): Returns a random integer from the range start (inclusive) to stop
(exclusive), optionally incremented by step.