-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjuly_18_python.py
More file actions
130 lines (108 loc) · 3.02 KB
/
july_18_python.py
File metadata and controls
130 lines (108 loc) · 3.02 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
#SLIDE 9
#Total Votes Calculation:
#If votes_candidate_A = 1500 and votes_candidate_B = 1300, then the total votes can be calculated as:
#pythtotal_votes results in 2800
votes_candidate_A = 1500
votes_candidate_B = 1300
total_votes = votes_candidate_A + votes_candidate_B
print(total_votes)
#SLIDE 10
#total_registered_voters = 10000, voters_voted = 6000, and additional_votes_by_mail = 500
total_registered_voters = 10000
voters_voted = 6000
additional_votes_by_mail = 500
turnout_percentage = ((voters_voted + additional_votes_by_mail) / total_registered_voters) * 100
print(turnout_percentage)
#SLIDE 11
#Impact of Variable Modification
#Modifying a value in one variable can affect calculations involving other variables.
x = 10
y = 5
sum = x + y # sum is 15
x = 20
sum = x + y # sum is now 25
print(sum)
#SLIDE 12
#Initial Data Assignment
total_voters = 10000
voters_voted = 6000
mail_in_votes = 500
voters_voted = voters_voted + mail_in_votes # voters_voted is now 6500
turnout_percentage = (voters_voted / total_voters) * 100 # turnout_percentage is now 65.0
print(turnout_percentage)
#Indexing Characters
#Characters in a string are identified by their index numbers, which can be positive or negative.
#Positive indexing starts at 0 from the beginning.
#Negative indexing starts at -1 from the end.
greeting = "Hello"
first_char = greeting[0] # 'H'
last_char = greeting[-1] # 'o'
print(first_char)
print(last_char)
#Concatenating Strings
#Combining first and last names:
first_name = "Jane"
last_name = "Smith"
full_name = first_name + " " + last_name # "Jane Smith"
print(full_name)
#Slicing Strings
#Extracting the area code from a phone number:
phone_number = "123-456-7890"
area_code = phone_number[0:3] # "123"
print(area_code)
#SLIDE 14
#Newline: "Hello\nWorld"
#Tab: "Hello\tWorld"
#Quote: "She said, \"Hello\""
varNewLine= "Hello\nWorld"
varTab= "Good bye\tMars"
varQuote= "She said, \"Laters\""
print(varNewLine+varTab+varQuote)
#SLIDE 17
# Voter Data Example
# Lists
# Storing names of registered voters:
# python
voters = ["John Doe", "Jane Smith", "Alice Brown"]
print(voters)
# Tuples
# Storing fixed coordinates of polling stations:
# python
station_coordinates = (40.7128, -74.0060)
print(station_coordinates)
# Dictionaries
# Storing voter information:
# python
voter_info = {"name": "John Doe", "age": 30, "city": "New York"}
print(voter_info)
# Sets
# Storing unique voter IDs:
# python
voter_ids = {101, 102, 103, 104}
print(voter_ids)
#SLIDE 18
# ELIF Statement
# Checks multiple conditions.
# Example:
# python
age = 20
if age >= 21:
print("You can vote.")
elif age >= 18:
print("You can pre-register to vote.")
else:
print("You are too young to vote.")
# OR Operator
# Checks if at least one condition is true.
# Example:
# python
has_voter_id = False
has_proof_of_residency = True
if has_voter_id or has_proof_of_residency:
print("Voter can proceed.")
# >>>SLIDE 21
try:
result = 10 / 0
except ZeroDivisionError:
print("Error: Cannot divide by zero")
print("Execution continues after handling the exception")