fruits = {
"apple": {
"color": "red or green",
"taste": "sweet or tart",
},
"banana": {
"color": "yellow",
"taste": "sweet",
},
"orange": {
"color": "orange",
"taste": "citrusy and sweet",
},
"grape": {
"color": "green or purple",
"taste": "sweet or tart",
},
"strawberry": {
"color": "red",
"taste": "sweet and slightly tart",
}
}
<IPython.core.display.Javascript object>
%%js
let codingLanguages = ["c++", "c", "c#", "python", "java", "javascript", "html", "css", "scss"]
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
let booleans = [true, false, true, true, true, false, false, false, false, false, true, true]
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
# Function to calculate the average of a list of grades
def calculate_average(grades):
# Sum all the grades and divide by the number of grades to get the average
return sum(grades) / len(grades)
# List to hold student information
students = []
# Example data for three students
student1 = {
'name': 'Alice',
'age': 17,
# Student's grades list
'grades': [90, 85, 88],
# Calculate the average grade using the function
'average_grade': calculate_average([90, 85, 88])
}
student2 = {
'name': 'Bob',
'age': 18,
'grades': [75, 80, 79],
'average_grade': calculate_average([75, 80, 79])
}
student3 = {
'name': 'Charlie',
'age': 16,
'grades': [92, 95, 89],
'average_grade': calculate_average([92, 95, 89])
}
# Append the students to the list
students.append(student1)
students.append(student2)
students.append(student3)
# Print student data including their names, ages, and average grades
print("Student Information:")
for student in students:
print(f"Name: {student['name']}, Age: {student['age']}, Average Grade: {student['average_grade']:.2f}")
Student Information:
Name: Alice, Age: 17, Average Grade: 87.67
Name: Bob, Age: 18, Average Grade: 78.00
Name: Charlie, Age: 16, Average Grade: 92.00
# Lyrics of "Guitar Man" by Bread (snippet)
lyrics = """
[Verse 1]
Well, I’m just a poor boy, I’ve got a guitar in my hand.
I’m looking for a place where I can sing my song.
[Chorus]
I’m a guitar man, I’m a guitar man, yeah.
I’m a guitar man, I’m a guitar man, yeah.
[Verse 2]
People say I’m lucky, but I don’t know what they mean.
The world is full of funny things, but I’m living my dream.
[Outro]
And when I play my guitar, it feels so fine.
I play my guitar, I feel so free.
"""
# Define the song title
song_title = "guitar man"
# Step 1: Count how many times the title appears in the lyrics
title_count = lyrics.lower().count(song_title.lower())
print(f"The title '{song_title}' appears {title_count} times in the lyrics.")
# Step 2: Find the index of the 50th word
words = lyrics.split() # Split the lyrics into individual words
if len(words) >= 50:
print(f"The 50th word is: '{words[49]}'") # 0-based index
else:
print("The lyrics contain less than 50 words.")
# Step 3: Replace the first verse with the last verse
# Splitting verses based on structure in the provided lyrics
lyrics_sections = lyrics.split("\n\n") # Assuming verses are separated by double newlines
if len(lyrics_sections) >= 2:
first_verse = lyrics_sections[0]
last_verse = lyrics_sections[-1]
# Replacing the first verse with the last verse
new_lyrics = lyrics.replace(first_verse, last_verse, 1)
print("\nLyrics after replacing the first verse with the last verse:\n")
print(new_lyrics)
else:
print("Not enough verses to perform the replacement.")
# Step 4: Concatenate two verses to make your own song
if len(lyrics_sections) >= 2:
custom_song = lyrics_sections[0] + "\n" + lyrics_sections[1]
print("\nCustom song by concatenating two verses:\n")
print(custom_song)
else:
print("Not enough verses to concatenate.")
The title 'guitar man' appears 4 times in the lyrics.
The 50th word is: 'I’m'
Lyrics after replacing the first verse with the last verse:
[Outro]
And when I play my guitar, it feels so fine.
I play my guitar, I feel so free.
[Chorus]
I’m a guitar man, I’m a guitar man, yeah.
I’m a guitar man, I’m a guitar man, yeah.
[Verse 2]
People say I’m lucky, but I don’t know what they mean.
The world is full of funny things, but I’m living my dream.
[Outro]
And when I play my guitar, it feels so fine.
I play my guitar, I feel so free.
Custom song by concatenating two verses:
[Verse 1]
Well, I’m just a poor boy, I’ve got a guitar in my hand.
I’m looking for a place where I can sing my song.
[Chorus]
I’m a guitar man, I’m a guitar man, yeah.
I’m a guitar man, I’m a guitar man, yeah.
%%js
// Step 1: Concatenating a flower with an animal
let flower = "Rose";
let animal = "Tiger";
let newCreature = flower + " " + animal;
// Step 2: Display the new creature using quotes and template literals
console.log("The new creature is: " + newCreature);
console.log(`The new creature is: ${newCreature}`);
// Step 3: Short story about the creature using variables and dialog
let creatureName = "Rose Tiger";
let creatureDescription = "a fierce yet beautiful creature with the grace of a flower and the strength of a beast.";
let story = `
Once upon a time, in a land of vibrant colors and wild imagination, there lived a creature called ${creatureName}.
It was known far and wide for its amazing abilities to charm and protect the forest.
One day, a curious adventurer stumbled upon the creature. The adventurer asked, "What are you, Rose Tiger?"
With a soft roar and a gentle rustling of its petals, the Rose Tiger replied, "I am both fierce and delicate,
able to fight and heal. But only if you respect the balance of nature."
The adventurer nodded, amazed by the creature's wisdom and beauty. And so, the Rose Tiger continued to watch over the forest,
its presence a reminder that strength and kindness can coexist."
`;
console.log(story);
<IPython.core.display.Javascript object>
import re
def analyze_text(text):
# Step 1: Display original string
print("Original String: " + text)
# Step 2: Count total characters (including spaces)
total_chars = len(text)
print(f"Total characters (including spaces): {total_chars}")
# Step 3: Find the longest word and its length
words = re.findall(r'\b\w+\b', text)
longest_word = max(words, key=len) if words else ""
print(f"Longest word: '{longest_word}' with {len(longest_word)} characters")
# Step 4: Reverse the string
reversed_text = text[::-1]
print("Reversed String: " + reversed_text)
# Step 5: Find middle word or character (excluding spaces and special characters)
clean_text = re.sub(r'[^A-Za-z0-9]', '', text) # Remove spaces and special characters
middle_index = len(clean_text) // 2
if len(clean_text) % 2 == 0:
middle_char = clean_text[middle_index - 1:middle_index + 1]
else:
middle_char = clean_text[middle_index]
print(f"Middle character(s): '{middle_char}'")
# Step 6: Add your own custom function - Count vowels
vowels = 'AEIOUaeiou'
vowel_count = sum(1 for char in text if char in vowels)
print(f"Total vowels: {vowel_count}")
# Step 7: Replace a word in the string
word_to_replace = input("Enter the word you want to replace: ")
if word_to_replace in text:
new_word = input(f"Enter the word you want to replace '{word_to_replace}' with: ")
modified_text = text.replace(word_to_replace, new_word)
print("Modified String: " + modified_text)
else:
print(f"'{word_to_replace}' not found in the original string.")
# Example usage
text = input("Enter your text: ")
analyze_text(text)
Original String: f
Total characters (including spaces): 1
Longest word: 'f' with 1 characters
Reversed String: f
Middle character(s): 'f'
Total vowels: 0
'd' not found in the original string.
<IPython.core.display.Javascript object>