-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_variables.py
More file actions
194 lines (153 loc) · 6.2 KB
/
01_variables.py
File metadata and controls
194 lines (153 loc) · 6.2 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
#!/usr/bin/env python3
"""
01_variables.py
This file demonstrates the four basic data types in Python:
- int (integers/whole numbers)
- float (floating-point/decimal numbers)
- string (text)
- bool (boolean - True or False)
Variables are like containers that store information.
You can create a variable by giving it a name and assigning it a value using =
"""
# ============================================================================
# INTEGER (int) - Whole numbers without decimal points
# ============================================================================
# Create an integer variable called 'age'
age = 25
print("Age:", age) # Output: Age: 25
print("Type of age:", type(age)) # type() tells us what kind of data it is
# Integers can be positive, negative, or zero
score = 100
temperature = -5
zero = 0
print("\nInteger examples:")
print("Score:", score)
print("Temperature:", temperature)
print("Zero:", zero)
# You can do math with integers
sum_result = 10 + 5 # Addition
difference = 10 - 3 # Subtraction
product = 4 * 7 # Multiplication
quotient = 20 // 4 # Integer division (gives whole number result)
print("\nMath with integers:")
print("10 + 5 =", sum_result)
print("10 - 3 =", difference)
print("4 * 7 =", product)
print("20 // 4 =", quotient)
# ============================================================================
# FLOAT (float) - Numbers with decimal points
# ============================================================================
# Create a float variable called 'pi'
pi = 3.14159
print("\n\nPi:", pi)
print("Type of pi:", type(pi))
# Floats are used when you need precision with decimals
height = 5.9 # Height in feet
price = 19.99 # Price in dollars
temperature_celsius = 36.6 # Body temperature
print("\nFloat examples:")
print("Height:", height, "feet")
print("Price: $", price)
print("Temperature:", temperature_celsius, "°C")
# Math with floats
result1 = 10.5 + 2.3 # Addition with floats
result2 = 7.5 - 1.2 # Subtraction with floats
result3 = 3.0 * 2.5 # Multiplication with floats
result4 = 9.0 / 2.0 # Division with floats (always gives float result)
print("\nMath with floats:")
print("10.5 + 2.3 =", result1)
print("7.5 - 1.2 =", result2)
print("3.0 * 2.5 =", result3)
print("9.0 / 2.0 =", result4)
# ============================================================================
# STRING (str) - Text data enclosed in quotes
# ============================================================================
# Create a string variable called 'name'
# You can use single quotes ' ' or double quotes " "
name = "Alice"
print("\n\nName:", name)
print("Type of name:", type(name))
# Strings can contain letters, numbers, spaces, and special characters
greeting = "Hello, World!"
address = '123 Main Street'
message = "I am learning Python!"
print("\nString examples:")
print(greeting)
print("Address:", address)
print(message)
# You can combine (concatenate) strings with +
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name # Notice the space in the middle
print("\nCombining strings:")
print("Full name:", full_name)
# You can repeat strings with *
laugh = "Ha" * 3 # Repeats "Ha" three times
print("Laugh:", laugh) # Output: HaHaHa
# Strings have many useful methods (functions that work on strings)
sentence = "python is awesome"
print("\nString methods:")
print("Original:", sentence)
print("Uppercase:", sentence.upper()) # Converts to UPPERCASE
print("Capitalized:", sentence.capitalize()) # Capitalizes first letter
print("Length:", len(sentence)) # len() gives the number of characters
# ============================================================================
# BOOLEAN (bool) - True or False values
# ============================================================================
# Create boolean variables
is_student = True
is_raining = False
print("\n\nIs student:", is_student)
print("Is raining:", is_raining)
print("Type of is_student:", type(is_student))
# Booleans are often the result of comparisons
is_greater = 10 > 5 # Is 10 greater than 5? True
is_equal = 7 == 7 # Is 7 equal to 7? True
is_less = 3 < 2 # Is 3 less than 2? False
print("\nBoolean comparisons:")
print("10 > 5:", is_greater)
print("7 == 7:", is_equal)
print("3 < 2:", is_less)
# More comparison operators:
print("\nMore comparisons:")
print("5 != 3:", 5 != 3) # != means "not equal to"
print("8 >= 8:", 8 >= 8) # >= means "greater than or equal to"
print("4 <= 2:", 4 <= 2) # <= means "less than or equal to"
# You can use logical operators: and, or, not
has_ticket = True
has_id = True
can_enter = has_ticket and has_id # Both must be True
print("\nLogical operators:")
print("Has ticket:", has_ticket)
print("Has ID:", has_id)
print("Can enter (needs both):", can_enter)
is_weekend = False
is_holiday = True
can_sleep_in = is_weekend or is_holiday # At least one must be True
print("Is weekend:", is_weekend)
print("Is holiday:", is_holiday)
print("Can sleep in (needs one):", can_sleep_in)
is_awake = False
is_asleep = not is_awake # not flips True to False and vice versa
print("Is awake:", is_awake)
print("Is asleep (opposite of awake):", is_asleep)
# ============================================================================
# VARIABLE NAMING RULES AND BEST PRACTICES
# ============================================================================
print("\n\n=== Variable Naming Tips ===")
print("✓ Use descriptive names: 'student_age' instead of 'x'")
print("✓ Use lowercase with underscores: 'first_name' not 'firstName'")
print("✓ Start with letter or underscore: 'name' or '_value'")
print("✗ Don't start with numbers: '1name' is invalid")
print("✗ Don't use Python keywords: 'if', 'for', 'while', etc.")
print("✗ Don't use spaces: 'my name' is invalid, use 'my_name'")
# ============================================================================
# SUMMARY
# ============================================================================
print("\n\n=== SUMMARY ===")
print("int: Whole numbers (no decimals)")
print("float: Numbers with decimals")
print("string: Text enclosed in quotes")
print("bool: True or False values")
print("\nYou can check any variable's type using type(variable_name)")
print("Variables can be reassigned to new values at any time!")