Posts

Showing posts from February, 2019

Python - Control Structure

An if statement looks like this: if expression : statements Notice the  colon  at the end of the expression in the  if  statement. Python uses indentation (white space at the beginning of a line) to delimit blocks of code. Other languages, such as C, use curly braces to accomplish this, but in Python indentation is mandatory; programs won't work without it. As you can see, the statements in the if should be indented. While i = 1 while i <=5 : print(i) i = i + 1 print("Finished!") The  infinite loop  is a special kind of while loop; it never stops running. Its condition always remains  True .  An example of an infinite loop: while 1==1: print("In the loop") This program would indefinitely print "In the loop".  You can stop the program's execution by using the Ctrl-C shortcut or by closing the program. Lists An empty list is created with an empty pair of square brackets. empty_list = [] print(emp...

Python - Comparisons AND Booleans

Booleans Another type in Python is the  Boolean  type. There are two  Boolean  values:  True  and  False . They can be created by comparing values, for instance by using the equal operator  == . >>> 1 == 1 and 2 == 2 True >>> 1 == 1 and 2 == 3 False Python uses words for its  Boolean  operators, whereas most other languages use symbols such as &&, || and !. Comparison ------------------------------ Python also has operators that determine whether one number ( float  or  integer ) is greater than or smaller than another. These operators are > and < respectively. >>> 8 > 6 True >>> 11 < 11 False ------------------------------ The greater than or equal to, and smaller than or equal to operators are >= and <=. They are the same as the strict greater than and smaller than operators, except that they return  True  when comparing e...