-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Variables_Data_Types Solved.py
More file actions
208 lines (123 loc) · 4.28 KB
/
Copy path2-Variables_Data_Types Solved.py
File metadata and controls
208 lines (123 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
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
# -*- coding: utf-8 -*-
"""
@author: Daniel García Angulo
Python exercises - Variables and Data Types
"""
#%%
'''Ex. 1: Create a program to store the given variables name and age'''
x = input('Give me your name please:')
y = input('now, give me your nationatily: ')
print('Nice to meet you,', x, 'is always good to meet a ', y)
#%%
'''Ex. 2: From the previous exercise, show the type of each variable…'''
type(x)
type(y)
#%%
'''Ex. 3: …and print the following sentence'''
# print the output
#%%
'''Ex. 4: Create a program to receive a year and return the age'''
x = int(input('Could you please provide me the year you were born?: '))
y = 2024 - x
print('Based on the year you were born you have', y, 'years old')
#%%
'''
Ex. 5: Create a program that given 1 integer and 1 float returns:
- The modulus as integer
- The floor division
- The cube of the integer number
'''
v = 4
l = 8
h = int(input('Give me a number: '))
v = int(input('and another one: '))
print('Based on both number here is the floor division: ',h // v, 'and the cube of the first number: ', h*v)
#%%
'''
Ex. 6: Create two variables:
empty_string variable with no text
sentence = "Python is awesome"
And calculate:
- The length of each variable.
- Write in uppercase the text from sentence
- Replace "Python" with "coding"
'''
e_st =''
sentence = 'Python is awesome'
len(e_st)
len(sentence)
print(sentence.lower())
print(sentence.upper())
sentence.replace('Python', 'coding')
#%%
'''
Ex. 7: Create a program to receive a sentence and return the text in lowercase and without spaces
'''
sentence = input('Give me a sentence: ').strip()
# Initialize an empty string to store the modified sentence
modified_sentence = ''
for char in sentence:
# Check if the character is not a space
if char != ' ':
# Convert the character to lowercase and add it to the modified sentence
modified_sentence += char.lower()
# Print the modified sentence
print("Modified Sentence (lowercase and without spaces):", modified_sentence)
#%%
'''
Ex. 8: Create the variable class_signature = 'Python exercises'.
- Get the first character
- Get the second to last character
- Get the fifth character
- Get the twentieth character
'''
class_signature = 'Python exercises'
class_signature[0]
class_signature[-2]
class_signature[4]
class_signature[20]
#%%
'''
Ex. 9: Create a program to slice both words from the previous variable class into 2 different variables
'''
class_signature ='Python exercises'
word1 = ''
word2 = ''
first_word_done = False
for char in class_signature:
if char != '' and not first_word_done:
word1 += char
elif char=='':
first_word_done = True
elif first_word_done:
word2 += char
print('first word', word1)
print('second word', word2)
#%%
'''
Ex. 10: Create a variable with the following text: “slicing not grouping” and extract the word “sing” from it with one code line.
'''
text = 'slicing not grouping'
text[5:9]
#%%
'''
Ex. 11: Create a program that count the number of times the letter “a” appear in a sentence ignoring upper or lowercases
'''
gili = 'I love to go to the park on saturdays and sundays'
gili_2 = gili.count('a')
print(gili_2)
#%%
'''
Ex. 12: Create a program that given 2 strings, return a new variable created with the first, middle and last letter of each string.
'''
# Define las cadenas de texto
first_str = 'Hello'
second_str = 'I love you'
# Obtiene la primera, la letra del medio y la última letra de la primera cadena
first_middle_last_first_str = first_str[0] + first_str[len(first_str)//2] + first_str[-1]
# Obtiene la primera, la letra del medio y la última letra de la segunda cadena
first_middle_last_second_str = second_str[0] + second_str[len(second_str)//2] + second_str[-1]
# Imprime la nueva variable creada para la primera cadena
print("Nueva variable creada para la primera cadena:", first_middle_last_first_str)
# Imprime la nueva variable creada para la segunda cadena
print("Nueva variable creada para la segunda cadena:", first_middle_last_second_str)