Professional Documents
Culture Documents
3. Arithmetic operators in
Python: + (addition), - (subtraction), * (multiplication), / (classic
division - always returns a float), % (modulus - divides left operand by
right operand and returns the remainder of the operation, e.g., 5 % 2 =
1), ** (exponentiation - left operand raised to the power of right
operand, e.g., 2 ** 3 = 2 * 2 * 2 = 8), // (floor/integer
division - returns a number resulting from division, but rounded down to
the nearest whole number, e.g., 3 // 2.0 = 1.0)
print(a)
print(b[0])
print(c)
print(d)
# int
a = 10
print(type(a))
# float
b = 9.87
print(type(b))
# str
c = "string"
print(type(c))
# list
d = [1, 2, 3, 4, 5]
print(type(d))
# bool
e = True
print(type(e))
# tuple
f = (1, "A", [1, 1, 1])
print(type(f))
# set
g = {1, 2, 2, 3, 3, 4, 5}
print(g) # doesn't show duplicates
# set ex: 1
countries = ["MD", "MD", "RO", "RU", "RO"]
countriesSet = set(countries)
countries = list(countriesSet)
print(countries)
# dict
person = {
"name": "Elena",
"age": 21
}
print(type(person))
print(person)
print(person['name'])
# None
A = None
print(type(A))
# we do not type null
CODE = "123"
print(CODE)
CODE = "1"
print(CODE)
# write a variable with uppercase it will mean constant
# if
if 2 > 0 and (3 > 0 or 1 == 1):
# if 1 > 0:
print("ok")
print("ok")
print("ok")
A = 10
if A is not None:
print("A: ", A)
X = 100
if X > 50:
print(f"X is {X}")
# exercise
if clientRate > 1:
print(f"{clientName} is a first buyer")
elif clientRate > 2:
print(f"{clientName} is a tactic buyer")
else:
print(f"{clientName} is an elit buyer")
myName = "Elena"
print(len(myName))
print(myName.upper())
print(myName.lower())
text = "the apple is green, the apple was green, the apple will always be
green"
print(text.count("apple"))
texts = ["green apple", "red apple", "yellow apple is the green basket" "the
apples are on 24/24"]
for text in texts:
if text.count("green") > 0:
print(text)
import numpy as np
zeros_arr = np.zeros(10)
print(zeros_arr)
rand_arr = np.random.rand(2, 2)
print(rand_arr)
# [[0.32634963 0.3499444 ]
# [0.52672999 0.80529246]]
# de la 0 pana la
rand_n_arr = np.random.randn(10)
print(rand_n_arr)
sum_arr = np.sum(reshaped_arr)
print(sum_arr)
# media aritmetica
mean_arr = np.mean(reshaped_arr)
print(mean_arr)
min_val = np.min(reshaped_arr)
print(min_val)
max_val = np.max(reshaped_arr)
print(max_val)
min_idx = np.argmin(reshaped_arr)
print(min_idx)
max_idx = np.argmax(reshaped_arr)
print(max_idx)
# x = [1, 1, 1, 2, 3, 3, 4, 5, 5, 5]
# uniqueX = np.unique(x)
# print(uniqueX)
x = [1, 1, 1, 2, 3, 3, 4, 5, 5, 5]
uniqueX, counts = np.unique(x, return_counts=True)
print(uniqueX)
print(counts)
is_present = np.isin(3, x)
print(is_present)
# FILTRU obisnuit:
xArr = [1, 2, 3, 4, 5]
oddNumbers = [a for a in xArr if a % 2 == 1]
print(oddNumbers)
class Person:
def __init__(self, name, lastname, birthYear, email, country):
self.name = name
self.lastname = lastname
self.birthYear = birthYear
self.email = email
self.country = country
class Student(Person):
def __init__(self, name, lastname, birthYear, email, country, school, grade,
friends):
super().__init__(name, lastname, birthYear, email, country)
self.school = school
self.grade = grade
self.friends = friends
def __str__(self):
return f"Student: {self.name} {self.lastname}\nBirth Year:
{self.birthYear}\nEmail: {self.email}\nCountry: {self.country}\nSchool:
{self.school}\nGrade: {self.grade}\nFriends: {', '.join(self.friends)}"
students = [
Student("Name", "Lastname", 2000, "email@gmail.com", "country",
"school", 9.99, ["Friend1", "Friend2"]),
Student("Ion", "Bors", 2004, "ionbors@mail.ru", "Moldova", "Best school",
5.55, []),
Student("Fang", "Ai", 1999, "fangaichinese@chinese.cn", "China", "China
school", 10,
["Mao Zedong", "Other chinese friend"]),
Student("John", "Doe", 2000, "john.doe@example.com", "USA", "Example
School", 10, ["Kim Chen", "American friend"]),
Student("Alice", "Smith", 1999, "alice.smith@example.com", "USA",
"School2", 9, ["Kim", "Fang Ai", "Ion Bors"]),
Student("Bob", "Johnson", 2001, "bob.johnson@example.com", "USA",
"School3", 7, ["Name Lastname"]),
Student("Eva", "Williams", 2002, "eva.williams@example.com", "USA",
"School4", 9.43, ["Friend1", "other friend"]),
Student("Emma", "Brown", 1998, "emma.brown@example.com", "USA",
"School5", 10, ["Italian Friend", "Adolph"]),
Student("Michael", "Davis", 2003, "michael.davis@example.com", "USA",
"School6", 9.7,
["Chinese Friend", "Friend3 with no name", "Friend4"]),
Student("Sophia", "Johnson", 2001, "sophia.johnson@example.com",
"USA", "School7", 8.34, ["Friend1"]),
Student("Liam", "Miller", 2000, "liam.miller@example.com", "USA",
"School8", 9.75,
["Emma Brown", "Sophia", "friend", "Last friend"])
# map (fun, array)
studentsNames = list(map(lambda student: student.name + "avich",
students))
print(studentsNames)
studentEmails = list(
map(lambda student: student.email + ' ' + student.name if
student.email.endswith(
"@gmail.com") else "Maybe fake email " + student.email, students))
print(studentEmails)
def lowerMarck(student):
student.grade = student.grade - 1
return student
class Car:
def __init__(self, mark, model, year):
self.mark = mark
self.model = model
self.year = year
cars = [
Car("Toyota", "Yaris", 2009),
Car("Toyota", "Camry", 2019),
Car("Audi", "Q5", 2023)
]
toyotaCars = list(filter(lambda c: c.mark == "Toyota", cars))
for car in toyotaCars:
print(f"Mark: {car.mark}, Model: {car.model}, Year: {car.year}")
# -> Mark: Toyota, Model: Yaris, Year: 2009
# -> Mark: Toyota, Model: Camry, Year: 2019
myFilters = list(
filter(lambda c:
(c.mark == "Toyota"
or c.mark == "Audi")
and c.year >= 2019, cars)
)
for car in myFilters:
print(f"Mark: {car.mark}, Model: {car.model}, Year: {car.year}")
# -> Mark: Toyota, Model: Camry, Year: 2019
# -> Mark: Audi, Model: Q5, Year: 2023
numbers = [1, 2, 3, 4, 5]
def sum(a, b):
return a + b
class Product:
def __init__(self, title, price):
self.title = title
self.price = price
products = [
Product("Banana", 25),
Product("Mere", 10),
Product("Rosii", 30)
]
for p in sortedProducts:
print(f"{p.title} {p.price}")
# -> Rosii 30
# -> Banana 25
# -> Mere 10
numbers = [1, 2, 3, 4, 5]
anyEven = any(n % 7 == 0 for n in numbers)
print(anyEven) # -> False
numbers = [-1, 1, 2, 3, 4, 5]
anyEven = any(n % 7 == 0 for n in numbers)
print(anyEven) # -> False
for i, s in enumerate(students):
print(f"{i} {s}")
# -> 0 Natalia
# -> 1 Bogdan
# -> 2 Igor
# -> 3 Mihaela
# -> 4 Marian
# combines 2 tables:
newDict = list(zip(students, ages))