eaglelandsonce commited on
Commit
6b43df8
·
verified ·
1 Parent(s): 06066e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+
4
+ # Set up the layout of the app
5
+ st.title("Intro to Python: Interactive Learning")
6
+ st.write("Welcome to the Python learning platform. Below are key examples to help you get started with Python programming.")
7
+
8
+ # Helper function to create an expandable explanation
9
+ def create_explanation(title, content):
10
+ with st.expander(f"Explanation: {title}"):
11
+ st.write(content)
12
+
13
+ # Example 1: Basic Syntax and Variables
14
+ st.subheader("1. Basic Syntax and Variables")
15
+ code1 = '''name = "Alice"
16
+ age = 25
17
+ height = 5.6
18
+
19
+ print(f"My name is {name}, I am {age} years old and {height} feet tall.")
20
+ '''
21
+ st.code(code1, language="python")
22
+ create_explanation("Basic Syntax", "This example shows how to assign values to variables, different data types, and how to print a formatted string.")
23
+
24
+ # Example 2: If-Else Condition
25
+ st.subheader("2. Control Flow: If-Else Statements")
26
+ code2 = '''temperature = 30
27
+
28
+ if temperature > 25:
29
+ print("It's a hot day.")
30
+ else:
31
+ print("It's a cool day.")
32
+ '''
33
+ st.code(code2, language="python")
34
+ create_explanation("If-Else", "This example demonstrates how to use conditional logic in Python to make decisions based on conditions.")
35
+
36
+ # Example 3: For and While Loops
37
+ st.subheader("3. Loops: For and While")
38
+ code3 = '''numbers = [1, 2, 3, 4, 5]
39
+ for number in numbers:
40
+ print(f"Number: {number}")
41
+
42
+ count = 5
43
+ while count > 0:
44
+ print(f"Countdown: {count}")
45
+ count -= 1
46
+ '''
47
+ st.code(code3, language="python")
48
+ create_explanation("Loops", "Loops are used to repeat tasks. A 'for' loop iterates over a list, and a 'while' loop repeats until a condition becomes false.")
49
+
50
+ # Example 4: Functions
51
+ st.subheader("4. Functions: Defining and Using Functions")
52
+ code4 = '''def add_numbers(a, b):
53
+ return a + b
54
+
55
+ result = add_numbers(10, 20)
56
+ print(f"The result is: {result}")
57
+ '''
58
+ st.code(code4, language="python")
59
+ create_explanation("Functions", "Functions allow you to define reusable blocks of code. In this example, a function adds two numbers.")
60
+
61
+ # Example 5: Lists and Dictionaries
62
+ st.subheader("5. Basic Data Structures: Lists and Dictionaries")
63
+ code5 = '''fruits = ["apple", "banana", "cherry"]
64
+ print(f"First fruit: {fruits[0]}")
65
+
66
+ student = {"name": "John", "age": 22, "major": "Computer Science"}
67
+ print(f"Student name: {student['name']}")
68
+ '''
69
+ st.code(code5, language="python")
70
+ create_explanation("Lists and Dictionaries", "Lists hold sequences of items, and dictionaries store data as key-value pairs.")
71
+
72
+ # Interactive code execution section
73
+ st.subheader("Try It Yourself: Python Code Simulation")
74
+ user_code = st.text_area("Paste your Python code below and hit 'Run' to see the output.", height=150)
75
+
76
+ if st.button("Run Code"):
77
+ try:
78
+ # Capture and display the output of the user's code
79
+ exec_locals = {}
80
+ exec(user_code, {}, exec_locals)
81
+ st.success("Code ran successfully!")
82
+ output = exec_locals
83
+ st.write(output)
84
+ except Exception as e:
85
+ st.error(f"Error: {str(e)}")
86
+
87
+ # Educational footer
88
+ st.write("This app is an educational tool to help you learn basic Python concepts. Keep exploring and practicing!")