Steemit.com
Python is a computer language that is designed by Guido van Rossum who is a Dutch programmer.
There are a lot of different parts of Python that we need to learn. These are some of them:
Boolean: a datatype that can have the value of True and False:
Example: 1 > 6 (False)
6 >= 9 = (False)
1 = 1 (True)
Comparision: the statements used to compare different numbers
Example:
== (equals)
!= (not equals)
< (less than)
<= (less than or equal to)
If statement: if the statement sets a condition, if the condition is true then do something.
Example:
If input > 100:
Print ‘You are the first customers!’
(Note: Indentation is really important in python, Every space and character should be structured in the required form)
Elif Statement: the statement that lets you set a lot more conditions than the If statement
Else Statement: the last statement in a condition
(Lessons about Loops)
For Loop: (Loop is to repeat something again and again) For loop allows you to repeat a thing over and again for a certain amount of times.
Break Statement: Just like the word sounds, Break Statements break the loops and stop it from running.
Continue: Continue stops one repetition and continues to the next one. It skips a loop when it meets a certain condition.
Range Function: Range function allows us to count in a huge range of numbers.
Applying Coding to Real Life Examples:
One of the challenges that our teacher gave us to work on is: Give the user 3 tries to guess a number between 1 and 10 (includes 1 and 10) that the computer created randomly.
If they guess correctly within 3 tries, they win. If they don’t, they lose.
This is what my code looks like to give the user 3 tries to guess the random number from the computers:
random_number = randint(1, 10)
attempt = 0
while attempt < 3:
guess = input(“Guess a number between 1 and 10 (included):”)
if guess == random_number:
print ‘You are correct! You win!’
break
else:
print ‘You are wrong. Sorry’
attempt += 1
print “Game Over”
(By running this code, the user can put 3 inputs, and the program will tell them if it is right or wrong. If they could not get in after 3 tries, they loses)