You are on page 1of 5

BASIC KEYS ON PYTHON

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.

5. Triple underscore prefix (e.g., '___method_name') is used to create name-mangled private


methods in Python classes.

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.

 Calculate constants such as pi, e, and the golden ratio.

 Round numbers up or down, and convert between degrees and radians.



Here's an example that demonstrates some of the functions provided by the math module:

import math

x = math.sqrt(16)
y = math.sin(math.pi/2)
z = math.ceil(3.7)
w = math.radians(45)

print(x) # Output: 4.0


print(y) # Output: 1.0
print(z) # Output: 4
print(w) # Output: 0.7853981633974483

OPERATORS

Python has several arithmetic operators for performing basic arithmetic operations. Here are the
most commonly used arithmetic operators in Python:

1. Addition (+): Adds two numbers together.


Example: 3 + 5 = 8

2. Subtraction (-): Subtracts one number from another.


Example: 8 - 3 = 5

3. Multiplication (*): Multiplies two numbers together.


Example: 3 * 5 = 15

4. Division (/): Divides one number by another.


Example: 15 / 3 = 5.0 (Note: the result is a float)

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

7. Exponentiation (**): Raises one number to the power of another.


Example: 3 ** 2 = 9

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:

1. The random module:


The random module provides functions for generating random numbers in Python. Here are
some examples of functions that the random module provides:

 random(): Returns a random float between 0 and 1.

Example:

colors = ['red', 'green', 'blue']


print(random.choice(colors))

 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))

 randint(a, b): Returns a random integer between a and b (inclusive).

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:

 ceil(x): Returns the smallest integer greater than or equal to x.

 floor(x): Returns the largest integer less than or equal to x.

 fabs(x): Returns the absolute value of x.

 factorial(x): Returns the factorial of x.

 fmod(x, y): Returns the remainder when x is divided by y.

 gcd(a, b): Returns the greatest common divisor of a and b.

 exp(x): Returns the exponential value of x.

 log(x[, base]): Returns the logarithm of x with the given base. If base is not specified, it defaults
to e.

 log10(x): Returns the base-10 logarithm of x.

 pow(x, y): Returns x raised to the power y.

 sqrt(x): Returns the square root of x.

 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.

 degrees(x): Converts x from radians to degrees.

 radians(x): Converts x from degrees to radians.

 pi: A constant that represents the value of pi.

 tau: A constant that represents the value of tau, which is twice the value of pi.

Other Funtions of RANDOM

Here are all the functions available in the Python random module:

 random(): Returns a random float in the range [0.0, 1.0).


 randint(a, b): Returns a random integer between a and b (inclusive).

 randrange([start], stop[, step]): Returns a random integer from the range start (inclusive) to stop
(exclusive), optionally incremented by step.

Two ways pag print f string ug dli f string

- Name= lady Marianne o bauyot


- (f “I am {name}”)

- If bawal mg f string ay ani pag print

You might also like