30-Days-Of-Python

Форк
0
247 строк · 7.0 Кб
1

2
# Single line comment
3
letter = 'P'                # A string could be a single character or a bunch of texts
4
print(letter)               # P
5
print(len(letter))          # 1
6
greeting = 'Hello, World!'  # String could be  a single or double quote,"Hello, World!"
7
print(greeting)             # Hello, World!
8
print(len(greeting))        # 13
9
sentence = "I hope you are enjoying 30 days of python challenge"
10
print(sentence)
11

12
# Multiline String
13
multiline_string = '''I am a teacher and enjoy teaching.
14
I didn't find anything as rewarding as empowering people.
15
That is why I created 30 days of python.'''
16
print(multiline_string)
17
# Another way of doing the same thing
18
multiline_string = """I am a teacher and enjoy teaching.
19
I didn't find anything as rewarding as empowering people.
20
That is why I created 30 days of python."""
21
print(multiline_string)
22

23
# String Concatenation
24
first_name = 'Asabeneh'
25
last_name = 'Yetayeh'
26
space = ' '
27
full_name = first_name  +  space + last_name
28
print(full_name) # Asabeneh Yetayeh
29
# Checking length of a string using len() builtin function
30
print(len(first_name))  # 8
31
print(len(last_name))   # 7
32
print(len(first_name) > len(last_name)) # True
33
print(len(full_name)) # 15
34

35
#### Unpacking characters 
36
language = 'Python'
37
a,b,c,d,e,f = language # unpacking sequence characters into variables
38
print(a) # P
39
print(b) # y
40
print(c) # t 
41
print(d) # h
42
print(e) # o
43
print(f) # n
44

45
# Accessing characters in strings by index
46
language = 'Python'
47
first_letter = language[0]
48
print(first_letter) # P
49
second_letter = language[1]
50
print(second_letter) # y
51
last_index = len(language) - 1
52
last_letter = language[last_index]
53
print(last_letter) # n
54

55
# If we want to start from right end we can use negative indexing. -1 is the last index
56
language = 'Python'
57
last_letter = language[-1]
58
print(last_letter) # n
59
second_last = language[-2]
60
print(second_last) # o
61

62
# Slicing
63

64
language = 'Python'
65
first_three = language[0:3] # starts at zero index and up to 3 but not include 3
66
last_three = language[3:6]
67
print(last_three) # hon
68
# Another way
69
last_three = language[-3:]
70
print(last_three)   # hon
71
last_three = language[3:]
72
print(last_three)   # hon
73

74
# Skipping character while splitting Python strings
75
language = 'Python'
76
pto = language[0:6:2] # 
77
print(pto) # pto
78

79
# Escape sequence
80
print('I hope every one enjoying the python challenge.\nDo you ?') # line break
81
print('Days\tTopics\tExercises')
82
print('Day 1\t3\t5')
83
print('Day 2\t3\t5')
84
print('Day 3\t3\t5')
85
print('Day 4\t3\t5')
86
print('This is a back slash  symbol (\\)') # To write a back slash
87
print('In every programming language it starts with \"Hello, World!\"')
88

89
## String Methods
90
# capitalize(): Converts the first character the string to Capital Letter
91

92
challenge = 'thirty days of python'
93
print(challenge.capitalize()) # 'Thirty days of python'
94

95
# count(): returns occurrences of substring in string, count(substring, start=.., end=..)
96

97
challenge = 'thirty days of python'
98
print(challenge.count('y')) # 3
99
print(challenge.count('y', 7, 14)) # 1
100
print(challenge.count('th')) # 2`
101

102
# endswith(): Checks if a string ends with a specified ending
103

104
challenge = 'thirty days of python'
105
print(challenge.endswith('on'))   # True
106
print(challenge.endswith('tion')) # False
107

108
# expandtabs(): Replaces tab character with spaces, default tab size is 8. It takes tab size argument
109

110
challenge = 'thirty\tdays\tof\tpython'
111
print(challenge.expandtabs())   # 'thirty  days    of      python'
112
print(challenge.expandtabs(10)) # 'thirty    days      of        python'
113

114
# find(): Returns the index of first occurrence of substring
115

116
challenge = 'thirty days of python'
117
print(challenge.find('y'))  # 5
118
print(challenge.find('th')) # 0
119

120
# format()	formats string into nicer output    
121
first_name = 'Asabeneh'
122
last_name = 'Yetayeh'
123
job = 'teacher'
124
country = 'Finland'
125
sentence = 'I am {} {}. I am a {}. I live in {}.'.format(first_name, last_name, job, country)
126
print(sentence) # I am Asabeneh Yetayeh. I am a teacher. I live in Finland.
127

128
radius = 10
129
pi = 3.14
130
area = pi # radius ## 2
131
result = 'The area of circle with {} is {}'.format(str(radius), str(area))
132
print(result) # The area of circle with 10 is 314.0
133

134
# index(): Returns the index of substring
135
challenge = 'thirty days of python'
136
print(challenge.find('y'))  # 5
137
print(challenge.find('th')) # 0
138

139
# isalnum(): Checks alphanumeric character
140

141
challenge = 'ThirtyDaysPython'
142
print(challenge.isalnum()) # True
143

144
challenge = '30DaysPython'
145
print(challenge.isalnum()) # True
146

147
challenge = 'thirty days of python'
148
print(challenge.isalnum()) # False
149

150
challenge = 'thirty days of python 2019'
151
print(challenge.isalnum()) # False
152

153
# isalpha(): Checks if all characters are alphabets
154

155
challenge = 'thirty days of python'
156
print(challenge.isalpha()) # True
157
num = '123'
158
print(num.isalpha())      # False
159

160
# isdecimal(): Checks Decimal Characters
161

162
challenge = 'thirty days of python'
163
print(challenge.find('y'))  # 5
164
print(challenge.find('th')) # 0
165

166
# isdigit(): Checks Digit Characters
167

168
challenge = 'Thirty'
169
print(challenge.isdigit()) # False
170
challenge = '30'
171
print(challenge.digit())   # True
172

173
# isdecimal():Checks decimal characters
174

175
num = '10'
176
print(num.isdecimal()) # True
177
num = '10.5'
178
print(num.isdecimal()) # False
179

180

181
# isidentifier():Checks for valid identifier means it check if a string is a valid variable name
182

183
challenge = '30DaysOfPython'
184
print(challenge.isidentifier()) # False, because it starts with a number
185
challenge = 'thirty_days_of_python'
186
print(challenge.isidentifier()) # True
187

188

189
# islower():Checks if all alphabets in a string are lowercase
190

191
challenge = 'thirty days of python'
192
print(challenge.islower()) # True
193
challenge = 'Thirty days of python'
194
print(challenge.islower()) # False
195

196
# isupper(): returns if all characters are uppercase characters
197

198
challenge = 'thirty days of python'
199
print(challenge.isupper()) #  False
200
challenge = 'THIRTY DAYS OF PYTHON'
201
print(challenge.isupper()) # True
202

203

204
# isnumeric():Checks numeric characters
205

206
num = '10'
207
print(num.isnumeric())      # True
208
print('ten'.isnumeric())    # False
209

210
# join(): Returns a concatenated string
211

212
web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
213
result = '#, '.join(web_tech)
214
print(result) # 'HTML# CSS# JavaScript# React'
215

216
# strip(): Removes both leading and trailing characters
217

218
challenge = ' thirty days of python '
219
print(challenge.strip('y')) # 5
220

221
# replace(): Replaces substring inside
222

223
challenge = 'thirty days of python'
224
print(challenge.replace('python', 'coding')) # 'thirty days of coding'
225

226
# split():Splits String from Left
227

228
challenge = 'thirty days of python'
229
print(challenge.split()) # ['thirty', 'days', 'of', 'python']
230

231
# title(): Returns a Title Cased String
232

233
challenge = 'thirty days of python'
234
print(challenge.title()) # Thirty Days Of Python
235

236
# swapcase(): Checks if String Starts with the Specified String
237
  
238
challenge = 'thirty days of python'
239
print(challenge.swapcase())   # THIRTY DAYS OF PYTHON
240
challenge = 'Thirty Days Of Python'
241
print(challenge.swapcase())  # tHIRTY dAYS oF pYTHON
242

243
# startswith(): Checks if String Starts with the Specified String
244

245
challenge = 'thirty days of python'
246
print(challenge.startswith('thirty')) # True
247
challenge = '30 days of python'
248
print(challenge.startswith('thirty')) # False

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.