Repository navigation
Expand file tree
/
Copy pathexamples.py
More file actions
166 lines (119 loc) · 4.28 KB
/
Copy pathexamples.py
File metadata and controls
166 lines (119 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Hello Python - Code Examples
This file contains working examples demonstrating your first Python concepts.
Run this file to see the output of each example.
Usage:
python3 examples.py
"""
# ==============================================================================
# Example 1: Your First Python Program
# ==============================================================================
print("=" * 50)
print("Example 1: Your First Python Program")
print("=" * 50)
# The print() function displays text to the screen
# Text must be enclosed in quotes (single or double)
print("Hello, World!")
print('This also works with single quotes!')
print() # Blank line for readability
# ==============================================================================
# Example 2: Printing Numbers and Multiple Values
# ==============================================================================
print("=" * 50)
print("Example 2: Printing Numbers and Multiple Values")
print("=" * 50)
# You can print numbers without quotes
print(42)
print(3.14)
# Print multiple values by separating with commas
print("The answer is:", 42)
print("Pi is approximately", 3.14159)
# Python automatically adds spaces between values
print("I", "love", "Python") # Output: I love Python
print()
# ==============================================================================
# Example 3: Using Comments
# ==============================================================================
print("=" * 50)
print("Example 3: Using Comments")
print("=" * 50)
# This is a single-line comment
# Comments help explain your code
# Python ignores everything after the # symbol
print("This line runs") # This comment is ignored
# You can "comment out" code to temporarily disable it
# print("This line will NOT run")
"""
This is a multi-line comment.
You can write multiple lines of explanation here.
Useful for documenting larger sections of code.
"""
print("Comments help make code readable!")
print()
# ==============================================================================
# Example 4: Creating Simple Output Patterns
# ==============================================================================
print("=" * 50)
print("Example 4: Creating Simple Output Patterns")
print("=" * 50)
# You can use print() multiple times to create patterns
print("*")
print("**")
print("***")
print("**")
print("*")
print() # Separator
# Or print special characters
print("Hello")
print("-----")
print("World")
print()
# ==============================================================================
# Example 5: The print() Function Options
# ==============================================================================
print("=" * 50)
print("Example 5: Advanced print() Usage")
print("=" * 50)
# By default, print() adds a newline at the end
# You can change this with the 'end' parameter
print("Hello", end=" ")
print("World") # This prints on the same line
# Output: Hello World
print()
# The 'sep' parameter changes how values are separated
print("A", "B", "C") # Default: separated by spaces
print("A", "B", "C", sep="-") # Separated by dash
print("2023", "12", "25", sep="/") # Output: 2023/12/25
print()
# ==============================================================================
# Example 6: Escape Characters
# ==============================================================================
print("=" * 50)
print("Example 6: Escape Characters")
print("=" * 50)
# Use \n for a new line
print("First line\nSecond line")
# Use \t for a tab
print("Name:\tJohn")
print("Age:\t25")
# Use \" to include quotes in your string
print("She said, \"Hello!\"")
# Use \\ to include a backslash
print("C:\\Users\\Documents")
print()
# ==============================================================================
# Summary
# ==============================================================================
print("=" * 50)
print("Summary")
print("=" * 50)
print("In this file, we covered:")
print("1. Basic print() function usage")
print("2. Printing numbers and multiple values")
print("3. Writing comments to document code")
print("4. Creating simple output patterns")
print("5. Advanced print() options (end, sep)")
print("6. Using escape characters for formatting")
print()
print("Try modifying these examples and see what happens!")
print("=" * 50)