list function :
- that is, a kind of object where you can get its elements one by one.
- iterable : string ,but integer is non-iterable
- empty lists
- len()
list_out_of_string = list('danger!')
print(list_out_of_string) # ['d', 'a', 'n', 'g', 'e', 'r', '!']
list_out_of_integer = list(235) # TypeError: 'int' object is not iterable
numbers = [1, 2, 3, 4, 5]
print(len(numbers)) # 5
empty_list = list()
print(len(empty_list)) # 0
single_element_list = ['danger!']
print(len(single_element_list)) # 1
multi_elements_list = list('danger!')
print(len(multi_elements_list)) # 7
empty_list_1 = list()
print(empty_list_1)
or
print(list())
name = ['Helen']
print(name)
loop
k = int(input())
total = 0
for i in range(1, k + 1):
total += i
print(total)
oceans = ['Atlantic', 'Pacific', 'Indian', 'Southern', 'Arctic']
for ocean in oceans:
print(ocean)
result :
Atlantic
Pacific
Indian
Southern
Arctic
for char in 'magic':
print(char)
m
a
g
i
c
times = int(input('How many times should I say "Hello"?'))
for i in range(times):
print('Hello!')
range
Let's look at the example below:
for i in range(5):
print(i)
What we'll get is this:
0
1
2
3
4
You can change the starting value if you’re not satisfied with 0, moreover, you can configure the increment (step) value by adding a third parameter:
for i in range(5, 45, 10):
print(i)
According to the parameters included, we’ve asked to print the numbers from 5 to 45 with an increment value of 10. Be careful again, the last value is not included in the output:
5
15
25
35
names = ['Rose', 'Daniel']
surnames = ['Miller']
for name in names:
for surname in surnames:
print(name, surname)
Rose Miller
Daniel Miller
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
for i in input():
print(digits[int(i)])
a = input()
word = ""
for i in a:
if i.islower():
word += i
elif i.isupper():
word += i.replace(i, '_' + i.lower())
print(word)
how to reverse string?
-
str=“Python”
reversedstring=’’.join(reversed(str)) -
word = input()
if word == word[::-1]:
print(“Palindrome”)
else:
print(“Not palindrome”)
while True:
nums = int(input())
if nums < 10:
continue
elif nums > 100:
break
print(nums)