#for loop
Explore tagged Tumblr posts
helloworldletscode · 8 months ago
Text
Iterations - for loop
Iteration, aka repeating, is a solution for tasks that need to be done over and over again.
Instead of writing dozens of lines of code for the same purpose, we can simplify it and shorten it to just a couple of lines. This way the code is both easier to read for the other programmers (fellow people hehe) and faster to process for the computer.
Also, simpler code reduces errors rate.
Examples of iterations are loops.
Looping means repeating something until a particular condition is satisfied. 
Python has 3 Basic Loops:
For Loop - used when we know number of iterations (repetitions) in advance.
While Loop - for situations where the number of iterations is unknown beforehand. 
Nested Loop - using one looping statement inside another looping statement.
For loop is used to execute the same instruction over and over again, a specific number of times.
for i in range(5):     print(“Hello!”) Output: Hello! Hello! Hello! Hello! Hello!
In the first line, we declared how many repetitions are needed. In the second line, we wrote what should be repeated a given number of times. In this case, we asked Python to print the string “Hello!” 5 times.
Basic structure of the for loop:
for i in range(5):     print(“Hello!”)
for - a keyword that signals that “for loop” is starting.
i - internal variable name which is keeping the counter value. Stands for “iteration”. We can read the whole line as “for 5 iterations/repetitions, please do the following:” For every loop, the 'i' variable increases by 1 because it's the counter. 'i' doesn't have to be 'i', we can switch it to another letter or another word, that are python approved for this (for example, you can’t use name of defined function instead of 'i').
#Loop using "unicorn" as internal variable, instead of "i" for unicorn in range(10): print(unicorn) #still works!
  in range() - represents the list of numbers we are looping through (number of time the iteration is running). Python starts the counter from 0. It means that range(5) -  will give a sequence of 5 numbers: 0, 1, 2, 3, 4 range() function has 3 parameters(start, end, steps), default values for start is 0 and step is 1. When we write range(5), we only give one parameter, and the function still works, because Python reads it as range(0,5,1) and the sequence starts with 0, increases by 5 counts, with step between each number being 1, by default.
We can change the parameters: range(1,20,3) this would result in iterations that starts from 1, goes up by 3 steps with the upper limit of 20: 1, 4,7,10,13,16,19.
Example: #print every 2 numbers (evens): for i in range (2, 10, 2):     print(x) output: 2 4 6 8 (!) output does not include 10 because 10 is the upper limit (result only includes number under 10)
: adding a colon sign in the end of the first line is mandatory, otherwise an error will occur.   Finally in the next line, we start writing the instruction, that is supposed to be repeated. This part isn’t starting right away, it should be indented. Indentation is the blank gap at the beginning of lines. Normal indentation is 4 spaces/tab long. Python would recognize 2 spaces or 4 spaces as an indentation, but 4 spaces length is more agreed upon and is used more wildly.
tip: How to write an instruction to get output of a list that starts from 1 instead of 0, accompanied by a string:
for i in range(10):     print(i+1, "I love you")
4 notes · View notes
skull-shore · 2 years ago
Text
july internship 1, Java
after getting to know the language itself and giving myself an overall understanding of it, i started with my first task:
Let your program print out every number from 1-100. If the number is dividable by 3, the program has to print "Fizz" instead of the number. If it is dividable by 5, it has to print "Buzz". If both (dividable by 3 & 5) apply, it has to print "FizzBuzz".
(if i explain smth wrong or if you know easier ways to solve that task, please feel free to comment or text me! i'm more than open for help!)
Well that's how i did it:
first off i went and created an integer variable "num" with the value 100
i built a for loop starting from 0 on and told the program to keep going until it went up to 100 ("<=" means i want it to stop at exactly 100, "<" would end at 99)
inside of the for loop i put a few if statements in
the 1st if tests whether the current number is dividable by 3 and 5
here i put "if (i % 5 == 0 && i % 3 == 0)"... why?
-> "i" is the variable for the number that is "being tested" rn. We want to find out whether it is dividable by 5. For that we use "%".
This means, let's say i=2. How often does 2 fit into 5? 2 times. This results to 4 (2+2=4, another 2 won't fit into 5). now 5-4=1 soooo 2 % 5 == 1. 1 is the rest.
So , back to my code: to find out if "i" is dividable by 5, i%5 has to be equal to 0. If i%5 has any rest, it means that it is not dividable by 5. Same with 3 ofc.
The && here just means "and".
so.. long story short: IF "i" is dividable by 5 AND 3 print "FizzBuzz" (instead of "i") and go on to the next number. if it is not, go into the next if statement
the next "else if" statements say: if it is not dividable by 5 AND 3, is it dividable by 3/ 5 only? IF yes, print "Fizz"/ "Buzz" and keep going with the next number, else: go into else statement
the last else statement says: if you didnt apply to the first few if statements, just print "i"
.. well and that's about it:) programming this one was really fun and i enjoyed sound it so much ahh🩷🩷🖤
Tumblr media Tumblr media
36 notes · View notes
nofilter-cory · 2 months ago
Text
Tumblr media
Dog
61K notes · View notes
thisiswhereikeepdcthings · 5 months ago
Text
This song has single-handedly taken over my life and it’s only been like a week
49K notes · View notes
maudiemoods · 11 months ago
Text
If life is a never ending loop of dirty dishes and laundry then that means life is a never ending loop of home cooked meals and comfy clean clothes
123K notes · View notes
wackom · 1 year ago
Text
Tumblr media
Bothering the beast
272K notes · View notes
frankenfran · 5 months ago
Text
whenever people say they can't watch/play/read anything slightly upsetting or scary or challenging because it's... upsetting or scary or challenging all i can think of is this lol
Tumblr media
45K notes · View notes
idlelizard · 3 months ago
Text
Tumblr media
twisting and bitturning
31K notes · View notes
homunculus-argument · 12 days ago
Text
No wait, random worldbuilding idea:
A people who have an age-old tradition, that when warriors left home to go to war, their family that remains home prepare funeral goods for them while they wait, sewing them the clothes and preparing the tools and all that they will be buried with - to emotionally prepare them to the hard possibility that the one who left will not return home alive. If the warrior returns, their burial goods are all burned in a bonfire that is lit for the celebration of their return.
And to this modern day, mothers of the culture will tell their children "fine, but let me take your measures for burial clothes before you go" as a way of telling them that something they're about to do is lethally stupid. Sharing stories about just how dramatic their mothers are, someone tells their group of friends that his mother once actually took out a measuring tape to start taking his measures when he said he's leaving home for a work trip.
And another one goes "pfft, yeah. This one time I went to a rock concert and came back home to mom sitting on her sewing machine, fucking making me a funeral coat."
13K notes · View notes
faitokie · 2 months ago
Text
Tumblr media
I know the truth hurts
16K notes · View notes
skullzy20 · 16 days ago
Text
Tumblr media Tumblr media
I want him dead (said with endearment)
18K notes · View notes
prokopetz · 5 months ago
Text
Well, the good news is that you've broken the time loop.
The bad news is that the reason you've broken the time loop is because whatever force was responsible for maintaining it looked at the shit you did on that particular iteration and thought to itself: "you know what would be really fucked up?"
15K notes · View notes
returntosunder · 12 days ago
Text
Tumblr media
I really liked Kris playing the piano
8K notes · View notes
nofilter-cory · 3 months ago
Text
Tumblr media
Cat
78K notes · View notes
thestuffedalligator · 1 year ago
Text
It is November of 1893. You have just killed a vampire. Exhausted and worn, you close your eyes and rest.
You wake up. It is May of 1893. You are on a train en route to Transylvania. Your diary says you have had queer dreams lately.
You try to believe it.
(An old woman puts a rosary in your hands. You accept it without question.)
You are a guest in a castle you have never been in before (you recognize every hallway and know without trying that every door is locked). Your host is a man you have never met before (you killed him you killed him you killed him he had turned to dust and there was blood on the snow).
One morning you cut yourself while shaving.
There is nobody behind you in the pocket mirror’s reflection.
You turn fast, and the razor is like a Kukri knife in your hand.
41K notes · View notes