Boolean and Logical Judgment¶
A boolean variable is of only two levels: True and False. This characteristic renders a boolean variable particularly suitable for making logical judgments. The operators for logical judgments include ==, !=, >, <, >=, and <=. See the following demonstration. Although they are different logical judgments, the outcomes of these judgments are always boolean: either True or False.
print('1',8==8)
print('2',8!=8)
print('3',2<0)
print('4',2<=2)
print('5','hello'=='hello')
print('6','Hello'=='hello')
1 True 2 False 3 False 4 True 5 True 6 False
Additionally, and , or, and not are three common boolean operators for logical judgments. The outcome of the a AND b is true, only when a is True and b is True as well. As long as one of them is false, the outcome of the AND judgment must be false. However, for the OR logical judgment, as long as one of the variables is True, the OR logical judgment must be True. See the below demonstration. The NOT operator changes the truth value of a variable from True to False and vice versa.
print('1',True and True)
print('2',True and False)
print('3',True or False)
print('4',False or False)
print('5',not True)
print('6',not False)
1 True 2 False 3 True 4 False 5 False 6 True
We can nest one logical judgment in another logical judgment. See as follow.
a=True
b=False
a==b or a!=b
True
A boolean operator is commonly used for process control. The idea is that according to the truth value of a boolean variable, different processes will be executed. One simple example is if else. In the following example, if the truth value of the variable name is true, then print out the content of name and print out a message otherwise.
name='apple'
if name=='apple':
print('hello apple')
else:
print('you are not apple.')
hello apple
We can make our script more flexible. For example, the below script will ask you to input a digit in between 0 and 9 and then return you a message whether the digit you have entered is an even number or odd number. The function input() always returns a string. Thus, we use int() to transform the variale digit from a string to an integer. The if else judgment can judge whether the digit entered by a user is an odd or even number. The operator % is used to get the remainder.
#digit=input() For compiling a markdown document. Students can remove # and use digit=input() instead of digit=8
digit=8
print('Please enter a digit between 0 and 9:',digit)
if(int(digit)%2==0):
print(digit+' is an even number')
else:
print(digit+' is an odd number')
Please enter a digit between 0 and 9: 8 8 is an even number
An advanced extension of if else is if elif else. The following example shows how it works.
v=50
if(v>100):
print('v > 100')
elif(v<=100 and v>80):
print('80 < v <= 100')
elif(v<=80 and v>50):
print('50 < v <= 80')
elif(v<=50 and v>10):
print('10 < v <= 50')
else:
print('v is smaller than 10')
10 < v <= 50
While loop¶
A loop in normal computer languages is a function to keep on doing a same kind of process until a criterion condition is met. The below example shows how a while function works.
count=0
while count<5:
print(count)
count=count+1
0 1 2 3 4
Another common function for executing a loop is for. Unlike while in which we only need to set up the condition to terminate it, in for, we need to set up the start and end points to control the epoches this for function will run. The function range(a,b) like the function seq(a,b) in R will return a serial of numbers from a to b. The first and second arguments of range() are the starting point and the end point of this numeric serial. The third argument of range() governs the interval between every two numbers in the serial.
for i in range(3,5):
print(i)
for i in range(3,9,2):
print(i)
3 4 3 5 7
Self Defined Functions¶
Every computer language provides users to develop their own functions to solve different problems. Every function is an independent module, which receives inputs from the environment or other functions, execute particular processes for these inputs, and generate products as outputs which are returnded to the working environment. A function can have its own variables, which basically cannot be accessed directly by other functions, unless their attributes are set as public (in C). In Python, we can create our own function by the operator def. The following example is a simple function. When declaring a function, we simply need to call def in Python. However, in other computer languages, a function is defined normally by { and }.
def my_fun():
print('This is my first function in Python')
my_fun()
This is my first function in Python
Now we can make a simple function to play a number-guessing game. Suppose users are told that they have to guess a number in between 1 and 10 in 7 times. On every trial, the function will tell you whether your guess is too high or too low.
import random
def guess():
print('You have 7 chancess to guess a number between 1 and 10')
ans=random.randint(1,10)
for i in range(1,7):
print('Guess:')
guess=int(input())
if(guess>ans):
print('Too high!')
elif(guess<ans):
print('Too low!')
else:
print('Bingo!')
break
#guess()