Filters

Hide filters
Platform
Content Type
Difficulty

All Tutorials · 15 Results

Contained in: Python for AI: A Crash Course strings
AI

Lesson in Python for AI: A Crash Course

Introduction to Python & Jupyter Notebooks

…type from the value assigned to it. Here are some examples: score = 200 # int time_travel_gigawatts = 1.21 # float greeting = "Welcome to Python" # string is_user_logged_in = True # boolean Note: In Python, boolean values are capitalized: True and False, not true and false. Python doesn’t have constants, just…
AI

Lesson in Python for AI: A Crash Course

Introduction to Python & Jupyter Notebooks

…heading The responses Dictionary. Below that cell is a code cell that defines a large dictionary named responses, where: Each key is a string that defines one of the kinds of user input that ELIZA will respond to. For example, if the user entered “I need a vacation,” that input…
AI

Lesson in Python for AI: A Crash Course

Working with Local Data (File Operations & Data Handling)

…file in "w" mode does — it creates the file before writing to it. Update the code in the cell so that each of the strings in the calls to file.write() ends with a newline (\n) character. It’ll end up looking like this: file = open("programming-languages.txt", "w") file.write("Programming Languages…
AI

Lesson in Python for AI: A Crash Course

Working with Local Data (File Operations & Data Handling)

…write the JSON file with its keys sorted in ascending alphabetical order, use this parameter and set its value to True. Working With JSON Strings In addition to providing ways to read and write to JSON files, the json module also provides methods for reading and writing data from…
AI

Lesson in Python for AI: A Crash Course

Working with Local Data (File Operations & Data Handling)

…list containing the entire contents of the file. Each element in the list is a line from the file, where a line is a string ending with a newline \n readlines() Returns a string containing the entire contents of the file. read() . • If not at the end of the file…
AI

Lesson in Python for AI: A Crash Course

Working with Online Data & APIs

…example use case is performing sentiment analysis on messages in online forums. You could GET the content of pages on a particular topic, use string methods to extract relevant information, and then either use that information to train a model or provide that information to an LLM for analysis…
AI

Lesson in Python for AI: A Crash Course

Python Syntax & Best Practices

…list’s contents, with each Checklistitem instance in the list represented by the output of the __repr__() method. Remember, __repr__() returns the developer-facing string representation of an instance. Go to the Checklistitem cell, comment out the __repr__() method, and run the cell. Run the # Initial checklist cell and then…
AI

Lesson in Python for AI: A Crash Course

Python Syntax & Best Practices

…priority` instance attribute has not previously been defined, define it. # Otherwise, simply update its value self.priority = priority def __str__(self): """Return a user-friendly string representation of the item.""" if self.checked: checkbox = "✅" else: checkbox = "⬜️" return f"{checkbox} {self.name} {self.priority_emoji()}({self.priority})" def __repr__(self): """Return a developer-facing string representation…
AI

Lesson in Python for AI: A Crash Course

Working with Local Data (File Operations & Data Handling)

…very pretty. Format the output with json.dumps() by running the following code: with open("programming-languages.json", "r") as file: data = json.load(file) json_string = json.dumps(data, indent=4) print(json_string) Now, the output is easily readable. Finally, there’s always a chance that the JSON could be poorly formatted, which…
AI

Lesson in Python for AI: A Crash Course

Working with Python Libraries & Package Management

Python projects: csv: Reading and writing CSV files. datetime: Working with dates and times. difflib: Computing differences in sequences. It’s especially useful for strings, lists, files, and web pages. json: Processing JSON data math: Math functions beyond basic arithmetic, especially “scientific” math. os: Miscellaneous operating system commands. random: Generating…
AI

Lesson in Python for AI: A Crash Course

Working with Local Data (File Operations & Data Handling)

…open("some-file.csv", "r") as file: reader = csv.reader(file) for row in reader: print(row) The code above prints each row as a list of strings. For example, if you had created the CSV file, some-file.csv, which contained the following: browser,vendor,year_appeared Chrome,Google,2008 Safari,Apple,2003 Edge…
AI

Lesson in Python for AI: A Crash Course

Working with Online Data & APIs

…usually made of the collection endpoint to which it belongs, followed by something that uniquely identifies the resource, such as an ID number or string. Here are some resource endpoints that would be used in the example e-commerce site: A customer with the ID abc123 would be accessed…
AI

Lesson in Python for AI: A Crash Course

Introduction to Python & Jupyter Notebooks

…quick explanation of the code you just entered and ran: The # symbol starts single-line comments in Python. Python’s print() function prints strings to the console. In Jupyter, anything printed to the console appears immediately after the code cell that generated that output. Markdown Cells Notebooks also have Markdown…
AI

Lesson in Python for AI: A Crash Course

Python Syntax & Best Practices

…this by defining a decorator function called enhance(): def enhance(func): """ This decorator makes the output of a function that returns a string a little more fancy. """ # Python allows nested functions! # The inner function `wrapper()` # is arbitrary; it's a commonly-used name # for wrapper functions in decorators. def wrapper…
AI

Lesson in Python for AI: A Crash Course

Python Syntax & Best Practices

…iterators. An iterable is an object that can return its elements one at a time. You’ve already seen some of Python’s iterables: strings, lists, tuples, dictionaries, and sets. A good general guide is that it’s an iterable if you can access an object’s elements…