Smyčka v pyhtonu

Příklady kódu

68
0

python smyčky

#x starts at 1 and goes up to 80 @ intervals of 2
for x in range(1, 80, 2):
  print(x)
12
0

pro v Pythonu

for i in range(1,10,2): #(initial,final but not included,gap)
  print(i); 
  #output: 1,3,5,7,9
  
for i in range (1,4): # (initial, final but not included)
  print(i);
  #output: 1,2,3 note: 4 not included

for i in range (5):
  print (i);
  #output: 0,1,2,3,4 note: 5 not included

python = ["ml","ai","dl"];  
for i in python:
  print(i);
  #output:  ml,ai,dl
  
for i in range(1,5):	#empty loop...if pass not used then it will return error
  pass; 	
  
8
0

smyčky v Pythonu

#While Loop:
while ("condition that if true the loop continues"):
  #Do whatever you want here
else: #If the while loop reaches the end do the things inside here
  #Do whatever you want here

#For Loop:
for x in range("how many times you want to run"):
  #Do whatever you want here
4
0

python pro smyčku

# A for loop in python is used to iterate (or loop) over a sequence.
# The sequence is usually range(start,end), but can also be a list.
# Example of for loop with range():
for num in range(1,10):
  print(num)
  # This prints all the numbers from 1 to 10, including 1 but excluding 10.

 # Here is an example of a for loop with a list:
lst = [1,2,3,4,5]
for item in lst:
  print(item)
  # This prints all items in the list.

# For loops are also used in list comprehensions.
lst = [1,2,3,4,5]
new_lst = [item for item in lst if i % 2 == 0]
# The list comprehension above generates a new list, which new_lst is put equal to.
# It iterates through the items in lst and only puts them in the new list if it is a multiple of 2.
1
0

python pro smyčku

#an object to iterate over 
items = ["Item1", "Item2", "Item3", "Item4"]

#for loop using range  
for i in range(len(items)):
  	print(items[i])
    
#for loop w/o using range    
for item in items:
  	print(item)

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................