-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path01_print.py
More file actions
223 lines (174 loc) · 6.81 KB
/
01_print.py
File metadata and controls
223 lines (174 loc) · 6.81 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
================================================================================
File: 01_print.py
Topic: The print() Function in Python
================================================================================
This file demonstrates the print() function, which is used to display output
to the console. It's one of the most fundamental functions in Python and
essential for debugging and displaying information.
Key Concepts:
- Basic printing
- Multiple arguments
- Separators and end characters
- Formatted strings (f-strings)
- Escape characters
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. Simple Output
# -----------------------------------------------------------------------------
# The most basic use of print()
print("--- Simple Output ---")
print("Hello, World!")
print("Welcome to Python!")
print("Learning is fun!")
# -----------------------------------------------------------------------------
# 2. Printing Different Data Types
# -----------------------------------------------------------------------------
# print() can output any data type
print("\n--- Different Data Types ---")
print(42) # Integer
print(3.14159) # Float
print(True) # Boolean
print(None) # NoneType
print([1, 2, 3]) # List
print({"a": 1}) # Dictionary
# -----------------------------------------------------------------------------
# 3. Printing Multiple Items
# -----------------------------------------------------------------------------
# Pass multiple arguments separated by commas
print("\n--- Multiple Items ---")
print("Hello", "World")
print("Python", "is", "awesome")
print("Name:", "Baraa", "| Age:", 25)
print(1, 2, 3, 4, 5)
# -----------------------------------------------------------------------------
# 4. The sep Parameter
# -----------------------------------------------------------------------------
# sep defines what goes between multiple items (default is space)
print("\n--- Separator Parameter ---")
print("Python", "Java", "C++", sep=", ")
print("2025", "01", "15", sep="-") # Date format
print("192", "168", "1", "1", sep=".") # IP address
print("apple", "banana", "cherry", sep=" | ")
print("a", "b", "c", sep="") # No separator
# -----------------------------------------------------------------------------
# 5. The end Parameter
# -----------------------------------------------------------------------------
# end defines what goes at the end (default is newline \n)
print("\n--- End Parameter ---")
print("Loading", end="")
print("...", end="")
print(" Done!")
print("First line", end=" --> ")
print("Second line")
# Creating a progress bar effect
print("\nProgress: ", end="")
for i in range(5):
print("█", end="")
print(" Complete!")
# -----------------------------------------------------------------------------
# 6. Variables in Print
# -----------------------------------------------------------------------------
# Print variable values
print("\n--- Variables ---")
name = "Baraa"
age = 25
city = "Gaza"
is_student = True
print("Name:", name)
print("Age:", age)
print("City:", city)
print("Student:", is_student)
# Print with variable calculations
x = 10
y = 5
print("Sum:", x + y)
print("Product:", x * y)
# -----------------------------------------------------------------------------
# 7. String Formatting - f-strings (Recommended)
# -----------------------------------------------------------------------------
# Modern way to embed variables in strings (Python 3.6+)
print("\n--- f-strings (Formatted String Literals) ---")
name = "Baraa"
age = 25
height = 1.75
print(f"My name is {name}")
print(f"I am {age} years old")
print(f"Next year I'll be {age + 1}")
# Formatting numbers
pi = 3.14159265359
print(f"Pi to 2 decimals: {pi:.2f}")
print(f"Pi to 4 decimals: {pi:.4f}")
# Padding and alignment
print(f"{'Left':<10}|{'Center':^10}|{'Right':>10}")
print(f"{1:<10}|{2:^10}|{3:>10}")
# Currency formatting
price = 1234.567
print(f"Price: ${price:,.2f}")
# -----------------------------------------------------------------------------
# 8. String Formatting - Other Methods
# -----------------------------------------------------------------------------
# Alternative formatting methods
print("\n--- Other Formatting Methods ---")
# .format() method
name = "Alice"
age = 30
print("Hello, {}! You are {} years old.".format(name, age))
print("Hello, {0}! You are {1} years old.".format(name, age))
print("Hello, {n}! You are {a} years old.".format(n=name, a=age))
# % operator (older style)
print("Hello, %s! You are %d years old." % (name, age))
# -----------------------------------------------------------------------------
# 9. Escape Characters
# -----------------------------------------------------------------------------
# Special characters using backslash \
print("\n--- Escape Characters ---")
print("Line 1\nLine 2\nLine 3") # \n = newline
print("Column1\tColumn2\tColumn3") # \t = tab
print("She said: \"Hello!\"") # \" = quote
print('It\'s a beautiful day') # \' = apostrophe
print("Path: C:\\Users\\Documents") # \\ = backslash
print("Bell sound: \a") # \a = bell (may not work)
# Raw strings - ignore escape characters
print("\nRaw string:")
print(r"C:\Users\Baraa\Desktop") # r prefix for raw string
# -----------------------------------------------------------------------------
# 10. Multi-line Printing
# -----------------------------------------------------------------------------
print("\n--- Multi-line Strings ---")
# Using triple quotes
message = """
This is a multi-line message.
It spans across several lines.
Very useful for long text!
"""
print(message)
# ASCII art example
print("""
╔═══════════════════════════╗
║ Welcome to Python! ║
║ Let's learn together! ║
╚═══════════════════════════╝
""")
# -----------------------------------------------------------------------------
# 11. Practical Examples
# -----------------------------------------------------------------------------
print("--- Practical Examples ---")
# Receipt example
print("\n========== RECEIPT ==========")
item1, price1 = "Coffee", 4.99
item2, price2 = "Sandwich", 8.50
item3, price3 = "Cookie", 2.25
total = price1 + price2 + price3
print(f"{item1:.<20}${price1:.2f}")
print(f"{item2:.<20}${price2:.2f}")
print(f"{item3:.<20}${price3:.2f}")
print("=" * 30)
print(f"{'TOTAL':.<20}${total:.2f}")
# Table example
print("\n| Name | Age | City |")
print("|----------|-----|------------|")
print(f"| {'Alice':<8} | {25:<3} | {'New York':<10} |")
print(f"| {'Bob':<8} | {30:<3} | {'London':<10} |")
print(f"| {'Charlie':<8} | {35:<3} | {'Tokyo':<10} |")