While loops are extremely useful when we need specific lines of code to run constantly.
Basically, we need to define a starting point, ending point, and how to jump from one point to another.
Of course, in between those points, some useful part of code need to be executed:
x = 0
while x < 10:
print("Value of x atm: ", x)
x = x + 1
Result:
Value of x atm: 0
Value of x atm: 1
Value of x atm: 2
Value of x atm: 3
Value of x atm: 4
Value of x atm: 5
Value of x atm: 6
Value of x atm: 7
Value of x atm: 8
Value of x atm: 9
>>>
We will import os module, so we can run application inside our operating system - using Python script.
"x = x + 1" is used to gradually jump from 0 until 3 is reached - it's like a very simple algorithm.
If you run this code, calculator will pop-up 3 times on Desktop. Sure, we can run other apps, not just calc:
import os
x = 0
while x <= 3:
os.system("calc")
x = x + 1
In next tutorial, we will learn about Break and Continue options when looping with While Loops.
With if statements we will do all kind of checks. For example, if x is smaller than y, we will just print: "x is smaller than y", and so on:
x = 20
y = 20
if x < y:
print("x is smaller than y")
if x > y:
print("x is bigger than y")
if x == y:
print("They are Equal")
Result:
They are Equal
>>>
We can add "else" at the end of check, as the last possible case. In the middle of check you can use "elif", which is short for "else if":
x = 20
y = 20
if x < y:
print("x is smaller than y")
elif x > y:
print("x is bigger than y")
else:
print("They are Equal")
Result:
They are Equal
>>>
It's fun to take input from keyboard:
x = input("Please enter x: ")
y = input("Please enter y: ")
if x < y:
print("x is smaller than y")
elif x > y:
print("x is bigger than y")
else:
print("They are Equal")
Result:
Please enter x: 555
Please enter y: 10
x is bigger than y
>>>
When you know that you will need numbers, and not strings, convert what you take from keyboard into integers/floats with int() or float() function.
x = input("Please enter x: ")
y = input("Please enter y: ")
print("Before x and y are strings: ")
print(type(x))
print(type(y))
print("After we convert them, they are integers: ")
x = int(x)
y = int(y)
print(type(x))
print(type(y))
Result:
Please enter x: 10
Please enter y: 50
Before x and y are strings:
<class 'str'>
<class 'str'>
After we convert them, they are integers:
<class 'int'>
<class 'int'>
>>>
Now, we are absolutely sure that things from keyboard will be used as numbers, and not as strings (words):
x = int(input("Please enter x: "))
y = int(input("Please enter y: "))
if x < y:
print("x is smaller than y")
elif x > y:
print("x is bigger than y")
else:
print("They are Equal")
Result:
Please enter x: 50
Please enter y: 100
x is smaller than y
>>>
In next tutorial, we will learn about While Loops.
Colors are: {'Green', 'Black', 'Yellow', 'Red'}
Number of colors: 4
--------------------------------------------------
After: {'Green', 'Black', 'Red'}
Number of colors: 3
>>>
Colors are: {'Green', 'Red', 'Yellow', 'Black'}
Number of colors: 4
--------------------------------------------------
After: {'Green', 'Red', 'Black'}
Number of colors: 3
>>>
Colors are: {'Black', 'Red', 'Green', 'Yellow'}
Number of colors: 4
--------------------------------------------------
After: set()
Number of colors: 0
>>>
Colors are: {'Green', 'Red', 'Black', 'Yellow'}
Traceback (most recent call last):
File "C:\Python38-64\ARCHIVE\learning-python.py", line 7, in <module>
print(colors)
NameError: name 'colors' is not defined
>>>
colors = {"Green", "Yellow", "Red", "Black", "Black"}
x = input("New Color for x: ")
y = input("New Color for y: ")
colors.update([x, y])
print(colors)
Result:
============== RESTART: C:\Python38-64\ARCHIVE\learning-python.py ==============
New Color for x: Magenta
New Color for y: Navy
{'Yellow', 'Black', 'Red', 'Green', 'Magenta', 'Navy'}
>>>
Function len() will work here, just as with strings or tuples:
If we grab value from keyboard, it's easy to find first location, and total number of elements:
tuple_names = ("Samantha", "Madonna", "Michael")
x = input("Enter name to check: ")
print("Number of them: ", tuple_names.count(x))
print("First occurence at position: ", tuple_names.index(x))
Result:
Enter name to check: Madonna
Number of them: 1
First occurence at position: 1
>>>
names =["Michael", "Samantha", "Anastasia", "John"]
#List destruction
del names
You can't print from something you destroyed:
names =["Michael", "Samantha", "Anastasia", "John"]
del names
print(names)
Result:
Traceback (most recent call last):
File "C:\Python38-64\ARCHIVE\learning-python.py", line 4, in <module>
print(names)
NameError: name 'names' is not defined
>>>
It's extremely easy to grab input from the keyboard in Python:
print("Your First App v0.00001")
print("-----------------------")
x = input("Please, enter your name: ")
print("Name: ", x)
We can do some immediate calculations:
print("Your First App v0.00001")
print("-----------------------")
x = int(input("Please, enter some number: "))
print("Result: ", x + x)
And sure, use as much inputs as you need:
print("Your First App v0.00001")
print("-----------------------")
x = (input("Please, enter your Name: "))
y = (input("Please, enter your LastName: "))
print(x, y)
In next tutorial we will work with Lists. They are just collections of elements enclosed in brackets.