From 93ac732a1b7cd78eb1d1531442528c589d5ef028 Mon Sep 17 00:00:00 2001 From: Manjit Date: Sun, 13 Oct 2024 14:47:38 +0530 Subject: [PATCH] new learning added --- learn.html | 15 ++++----------- script.js | 52 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/learn.html b/learn.html index f57b4eb..d91d712 100644 --- a/learn.html +++ b/learn.html @@ -5,17 +5,13 @@ Math 4 Python - Learn - - - - +
@@ -46,6 +38,7 @@

Python Lessons

  • Conditionals
  • Loops
  • Functions
  • +
  • Sets
  • @@ -65,4 +58,4 @@

    Python Lessons

    - \ No newline at end of file + diff --git a/script.js b/script.js index 9cc8686..f7f3479 100644 --- a/script.js +++ b/script.js @@ -26,19 +26,19 @@ function showEditor(lesson) { // Update the editor content based on the lesson chosen if (lesson === 'Hello World') { - editor.setValue(` + editor.setValue(`\ # Lesson: Hello World in Python # The print() function is used to display output to the console. -print("Hello, World!")`, 1); // 1 to move cursor to the end +print("Hello, World!")`, 1); } else if (lesson === 'Variables') { - editor.setValue(` + editor.setValue(`\ # Lesson: Variables in Python # Variables are used to store data. x = 10 # This is an integer variable name = "Alice" # This is a string variable -print(x, name)`, 1); // 1 to move cursor to the end +print(x, name)`, 1); } else if (lesson === 'Conditionals') { - editor.setValue(` + editor.setValue(`\ # Lesson: Conditionals in Python # Conditionals allow you to make decisions in your code. x = 10 @@ -47,19 +47,45 @@ if x > 5: else: print("x is not greater than 5")`, 1); } else if (lesson === 'Loops') { - editor.setValue(` + editor.setValue(`\ # Lesson: Loops in Python # Loops allow you to repeat code multiple times. for i in range(5): print(f"This is iteration {i+1}")`, 1); } else if (lesson === 'Functions') { - editor.setValue(` + editor.setValue(`\ # Lesson: Functions in Python # Functions allow you to group code that performs a specific task. def greet(name): return f"Hello, {name}!" print(greet("Alice"))`, 1); + } else if (lesson === 'Sets') { // New lesson added here + editor.setValue(`\ +# Lesson: Sets in Python +# Sets are collections of unique elements. +my_set = {1, 2, 3, 4, 5} +print("Original set:", my_set) + +# Adding an element +my_set.add(6) +print("After adding 6:", my_set) + +# Removing an element +my_set.remove(3) +print("After removing 3:", my_set) + +# Checking if an element exists +if 2 in my_set: + print("2 is in the set") + +# Set operations +another_set = {4, 5, 6, 7} +union_set = my_set.union(another_set) +print("Union of sets:", union_set) + +intersection_set = my_set.intersection(another_set) +print("Intersection of sets:", intersection_set)`, 1); } } @@ -73,7 +99,7 @@ async function runCode() { // Run the code using Pyodide try { - await pyodide.runPythonAsync(` + await pyodide.runPythonAsync(`\ import sys from io import StringIO @@ -92,7 +118,6 @@ sys.stdout = sys.__stdout__ output = output_buffer.getvalue() output `).then(output => { - // console.log(output); terminal.innerHTML += output; }); } catch (err) { @@ -100,7 +125,6 @@ output } } - function clearTerminal() { document.getElementById('terminal').innerHTML = ''; } @@ -137,7 +161,7 @@ function viewChallenge(challengeName) { switch (challengeName) { case 'Basic Math Challenge': - description.innerHTML = ` + description.innerHTML = `\

    Basic Math Challenge

    Test your skills in basic arithmetic operations. Solve the problems below: