- It represents an isolated environment to manage dependencies without them conflicting with other projects.
- In the root of your new project, run the following command in your terminal:
python -m venv .venv
- After entering venv, install the project dependencies (modules, libs in general)
- To exit venv, simply execute the following command, or close the terminal.
deactivate
- To install the requests module, run:
pip install requests
- To view the dependencies and their installed versions:
pip freeze
- Store all installed dependencies:
pip freeze > requirements.txt
- Dictionaries in python is a type of structure that stores information in key and value pairs. Each key is unique for accessing and selecting the corresponding value.
person = {
"name": "Mattheus",
"age": 21,
"contact": "https://www.linkedin.com/in/mattheuspereira/"
}
- Dictionaries are useful for organizing and retrieving data efficiently.
print(f"Name: { person['name'] }")
print(f"Age: { person['age'] }")
print(f"Contact: { person['contact'] }")
Note that with a single person variable, we can store different types of information, such as name, age and contact.
- We can also build dictionary lists and display the information through loops:
people = [
{ "name": "Mattheus", "age": 27 },
{ "name": "Jessica", "age": 32 },
{ "name": "Lucas", "age": 18 },
]
for p in people:
print(f"Name: { p['name'] }, Age: { p['age'] }")
Name: Matheus, Age: 21
Name: Jessica, Age: 28
Name: Lucas, Age: 30
We can add new information to the dictionary as follows:
player = {
"name": "Mattheus",
"level 1
}
player['life'] = 100
player['damage'] = 25.2
player['clan'] = "Pythonists"
player['leader_clan'] = True
print(player)
###Exit
{
'name': 'Mattheus',
'level 1,
'life': 100,
'damage': 25.2,
'clan': 'Pythonists',
'leader_clan': True
}
##Conditionals
- Changes the execution flow of our programs
if (condition) // do something
or else (other condition) // do something
otherwise // do something
-
The condition must be true for the instruction block to be executed.
-
We can use operators, variables, and even call functions with returns to validate a conditional.
age = 18
if age >= 18
print("adult")
elif age >=16:
print("teen")
elif age >=13:
print ("pre teenager")
else:
print("child")
In the example above, I created a variable called age, which takes the value of 18.
Then I created some conditions to display a message
-
If the age is greater than or equal to 18: adult
-
Or, if the age is greater than or equal to 16: teenager
-
Or, if the age is greater than or equal to 13: pre-adolescent
-
Otherwise: child
-
These are ordered collections of items in Python
-
The indices in a list can be of different types and can be accessed through indexes.
´´´ fruits = ['apple', 'banana', 'orange'] print (fruits[0]) ´´´
-
'for' and 'while' are control structures used to execute a block of code multiple times.
-
'for' is generally used when the number of iterations is known in advance.
-
'while is when the stopping condition is not known in advance.
for number in range (5): print(number)
'while' example in python
counter = 0 while counter <5: print (counter) counter +=1
-
Dictionaries are data structures that store key-value pairs
-
Each value is associated with a unique key, allowing quick access to data.
´´´student={'name': 'João', 'age':20, 'course': 'Engineering'} print(student['age'])
-
Functions are blocks of code that perform a specific task and can be reused in different parts of the program.
-
Parameters are values that can be passed to a function to customize its behavior.
´´´ def greeting(name): print(f''Hello, {name}'')
greeting('Maria') ´´´