In Python, the print()
function is used to display output to the console.
print("Hello, World!")
Variables are used to store data values.
name = "John"
age = 30
Lists are ordered collections of items.
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Outputs 'banana'
We can add to a list:
fruits = ['apple','banana','cherry']
fruits.append('grapes')
print (fruits[3]) # Outputs 'grapes'
# to remove
fruits.pop(3)
# to see how many items we have we use the len() function
len(fruits)
both strings and function returns can be appended.
There are several looping techniques in Python.
Loops through a range or list of items...
for i in range(3):
print(i) # Outputs '0', '1', '2'
fruits = ['apple','banana','cherry']
for fruit in fruits:
print (fruit) # Outputs 'apple','banana','cherry'
Loops forever while a condition is still true until it is false
count = 0
while count < 3: ## loop while the count is less than 3 (Condition is TRUE)
print(count)
count += 1 # Outputs '0', '1', '2'
Dictionaries are used to store data as key-value pairs. Lists can also contain dictionaries as you will see in this course
person = {
"name": "John",
"age": 30,
"city": "New York"
}
print(person["name"]) # Outputs 'John'
people = [
{
"name": "John",
"age": 30,
"city": "New York"
},
{
"name": "Daisy",
"age": 30,
"city": "New Jersey"
}
]
for person in people: # For each dictionary represented as person in the list of people
print (person['name']
## This will loop through the list and print out the names.
Modules are pre-written Python code that can be "imported" into your scripts.
import math
print(math.sqrt(16)) # Outputs '4.0'
Functions are blocks of code that only run when called.
def greet(name):
return "Hello, " + name
print(greet("John")) # Outputs 'Hello, John'
Classes provide a blueprint for creating objects.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return "Woof!"
my_dog = Dog("Buddy")
print(my_dog.bark()) # Outputs 'Woof!'
To contact APIs, we typically use the requests
module.
import requests
response = requests.get('https://api.example.com/data')
data = response.json()
print(data)
Note: Ensure the requests
module is installed using pip install requests
.
Managing sensitive credentials and configurations outside of your codebase is crucial for security. The python-dotenv
library, combined with Python's built-in os
module, offers a convenient way to load environment variables from a .env
file into the environment and then access them in your Python scripts.
First, you need to install the python-dotenv
package:
pip install python-dotenv
Create a .env
file in the root directory of your project and add your environment variables:
DATABASE_URL=your_database_url
SECRET_KEY=your_secret_key
Note: Always add .env
to your .gitignore
file to ensure that it's not committed to version control.
To load the variables from the .env
file:
import os
from dotenv import load_dotenv
load_dotenv()
The load_dotenv()
function will read the .env
file and populate the environment variables.
Use os.getenv()
to retrieve the value of an environment variable:
database_url = os.getenv("DATABASE_URL")
secret_key = os.getenv("SECRET_KEY")
If the environment variable is not set, os.getenv()
will return None
. To provide a default value, pass it as the second argument:
default_url = "default_database_url"
database_url = os.getenv("DATABASE_URL", default_url)
- Security: Never commit your
.env
file or any sensitive keys to version control. - Documentation: Always provide a
.env.example
orREADME
documentation indicating which environment variables are required for your application to run. - Flexibility: Using environment variables allows you to easily switch between different configurations, making your application more flexible and adaptable to different environments (development, testing, production).
python-dotenv
and os
provide a simple and secure way to manage environment variables in Python applications. By separating configurations and secrets from the code, you enhance the security and flexibility of your projects.
CSV (Comma-Separated Values) is a common format for data interchange. Python's built-in csv
module makes it easy to read and write data in CSV format.
To work with CSV files, we first need to import the csv
module.
import csv
Here's how you can read data from a CSV file:
with open('filename.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
This will print each row as a list. If the CSV file has headers, the first row will be the header.
To write data to a CSV file:
data = [
["Name", "Age", "City"],
["John", 30, "New York"],
["Marie", 22, "Boston"],
["Mike", 32, "Chicago"]
]
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
This will create (or overwrite) a file named output.csv
with the provided data.
If you prefer to work with dictionaries instead of lists, csv
provides DictReader
and DictWriter
for this purpose.
with open('filename.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
This will print each row as an ordered dictionary.
data = [
{"Name": "John", "Age": 30, "City": "New York"},
{"Name": "Marie", "Age": 22, "City": "Boston"},
{"Name": "Mike", "Age": 32, "City": "Chicago"}
]
headers = ["Name", "Age", "City"]
with open('output.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=headers)
writer.writeheader() # writes the headers
writer.writerows(data)
The csv
module provides a simple interface to read and write CSV files. For more advanced functionalities (like handling different delimiters, quoting characters, etc.), you can refer to the official Python documentation.
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Python's built-in json
module provides methods to encode and decode data in JSON format.
Convert a JSON string into a Python object:
import json
data = '{"name": "John", "age": 30, "city": "New York"}'
parsed_data = json.loads(data)
print(parsed_data["name"]) # Outputs 'John'
Read JSON data from a file and convert it into a Python object:
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
Convert a Python object into a JSON string:
import json
person = {
"name": "John",
"age": 30,
"city": "New York"
}
json_string = json.dumps(person)
print(json_string)
Write a Python object as JSON data to a file:
import json
person = {
"name": "John",
"age": 30,
"city": "New York"
}
with open('output.json', 'w') as file:
json.dump(person, file)
You can use the indent
parameter in dumps()
or dump()
to make the output more readable:
json_string = json.dumps(person, indent=4)
print(json_string)
To ensure keys in the output are sorted, use the sort_keys
parameter:
json_string = json.dumps(person, indent=4, sort_keys=True)
print(json_string)
The json
module in Python provides a straightforward way to encode and decode data in JSON format, making it easy to work with JSON data sources or APIs.