HyperLearning AI - Introduction to Python

An introductory course to the Python 3 programming language, with a curriculum aligned to the Certified Associate in Python Programming (PCAP) examination syllabus (PCAP-31-02).
https://knowledgebase.hyperlearning.ai/courses/introduction-to-python

02. Control and Evaluations Part 1

https://knowledgebase.hyperlearning.ai/courses/introduction-to-python/modules/2/control-and-evaluations-part-1

In this module, we will cover the fundamental building blocks of the Python programming language, namely:

  • Basic Concepts - logical/physical lines, comments, indentation, identifiers and keywords
  • Literals - string, boolean, numeric and special literals
  • Operators - unary, binary, arithmetic, assignment, comparison, logical, identity, membership and bitwise operators

1. Basic Concepts

1.1. Logical and Physical Lines

In [6]:
# Logical line spanning a single physical line
my_string = 'My entire string on a single physical line.'
print(my_string)
My entire string on a single physical line.

1.2. Explicit Line Joining

In [8]:
# String literal spanning two physical lines using explicit line joining
my_string = 'The first part of the string. \
The second part of the string'
print(my_string)
The first part of the string. The second part of the string
In [15]:
# Control flow spanning multiple physical lines using explicit line joining
year = 2019
month = 9
day = 15
if 1900 < year < 2100 and 1 <= month <= 12 \
    and 1 <= day <= 31:    # Valid date
        print("You have entered a valid date")
You have entered a valid date

1.3. Implicit Line Joining

In [19]:
# Implicit line joining
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',    # Weekdays
        'Saturday', 'Sunday')    # Weekend
print(days)
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

1.5. Multi-Logical Physical Lines

In [32]:
# Multiple logical lines spanning a single physical line
from datetime import date; today = date.today(); print(today)
2019-09-17

1.7. Indentation

In [24]:
# Identation is required for control flow in Python
x = 101
if x >= 100:
    print("Your number is bigger than or equal to 100")
else:
    print("Your number is less than 100")
Your number is bigger than or equal to 100
In [25]:
# Incorrect indentations lead to IndentationError
x = 100
    y = 200    # Incorrect indentation
if x >= y:
print("X is bigger than or equal to Y")    # Incorrect indentation 
else:
    print("Y is bigger than X")
  File "<ipython-input-25-ef612de651e5>", line 3
    y = 200    # Incorrect indentation
    ^
IndentationError: unexpected indent

1.8. Identifiers

In [26]:
# Valid identifiers
my_first_number = 100
my_first_string = 'Hello World'
In [31]:
# Invalid identifiers
1number = 10
string-test = 'Invalid Identifier'
  File "<ipython-input-31-d040c6f74b4c>", line 3
    string-test = 'Invalid Identifier'
                                      ^
SyntaxError: can't assign to operator

2. Literals

2.1. String Literals

In [35]:
my_first_string = 'Hello World'
my_second_string = 'Line 1.\nLine 2.'
my_third_string = r'Line 1.\nLine 2.'
my_fourth_string = u'r\u00e9sum\u00e9'

print(my_first_string)
print(my_second_string)
print(my_third_string)
print(my_fourth_string)
Hello World
Line 1.
Line 2.
Line 1.\nLine 2.
résumé

2.2. Boolean Literals

In [37]:
my_first_boolean = True
my_second_boolean = False

print(my_first_boolean)
print(my_second_boolean)
print(1==True)
print(1==False)
print(0==True)
print(0==False)
True
False
True
False
False
True

2.3.1. Integer Literals

In [47]:
# Integer literals using different number systems
decimal_integer = 100
binary_integer = 0b1100100
octal_integer = 0o144
hexadecimal_integer = 0x64
decimal_groupings_integer = 100_000_000

print(decimal_integer)
print(binary_integer)
print(octal_integer)
print(hexadecimal_integer)
print(decimal_groupings_integer)
100
100
100
100
100000000

2.3.2. Floating Point Literals

In [46]:
# Floating point literals
my_first_number = 3.14
my_second_number = 10e2
my_third_number = 100e-5
my_fourth_number = 10.
my_fifth_number = .12345
my_sixth_number=3.14_15_93

print(my_first_number)
print(my_second_number)
print(my_third_number)
print(my_fourth_number)
print(my_fifth_number)
print(my_sixth_number)
3.14
1000.0
0.001
10.0
0.12345
3.141593

2.3.3. Imaginary Literals

In [56]:
# Imaginary literals
my_first_complex_number = 10j
my_second_complex_number = .10j
my_third_complex_number = 2+3j

print(my_first_complex_number)
print(my_second_complex_number, 
      my_second_complex_number.real, 
      my_second_complex_number.imag)
print(my_third_complex_number, 
      my_third_complex_number.real, 
      my_third_complex_number.imag)
10j
0.1j 0.0 0.1
(2+3j) 2.0 3.0

2.4. Special Literals

In [63]:
x = None
print(x)
if x:
    print('x is True')
else:
    print('x is not True')
print(bool(None))
None
X is not True
False

3. Operators

3.1. Arithmetic Operators

In [66]:
# Addition
print(13 + 7)

# Subtraction
print(10 - 7)

# Multiplication
print(64 * 8)

# Division
print(225 / 15)

# Modulus (remainder after division)
print(69 % 8)

# Exponentiation (raising by a power)
print(2 ** 5)

# Floor division (quotient)
print(100 // 7)
20
3
512
15.0
5
32
14

3.2. Assignment Operators

In [111]:
# Assignment
x = 10
print(x)

# Add and assign (x = x + 5)
x += 5
print(x)

# Substract and assign (x = x - 2)
x -= 2
print(x)

# Multiply and assign (x = x * 4)
x *= 4
print(x)

# Divide and assign (x = x / 2)
x /= 2
print(x)

# Modulus and assign (x = x % 4)
x %= 4
print(x)

# Exponentiation and assign (x = x ** 8)
x **= 8
print(x)

# Floor division and assign (x = x // 15)
x //= 15
print(x)

# Bitwise AND and assign (x = x & 18)
x = int(x)
x &= 18
print(x)

# Bitwise OR and assign (x = x | 10)
x |= 10
print(x)

# Bitwise XOR and assign (x = x ^ 2)
x ^= 2
print(x)

# Bitwise signed right shift and assign (x = x >> 1)
x >>= 1
print(x)

# Bitwise zero fill left shift and assign (x = x << 2)
x <<= 2
print(x)
10
15
13
52
26.0
2.0
256.0
17.0
16
26
24
12
48

3.3. Comparison Operators

In [93]:
# Equal
print(10 == 100)

# Not equal
print(2 != 5)

# Greater than
print(13 > 12)

# Less than
print(100 < 1_000_000)

# Greater than or equal to
print(15 >= 15)

# Less than or equal to
print(20 <= 19)
False
True
True
True
True
False

3.4. Logical Operators

In [94]:
# AND
print(1 < 100 and 10 < 100)

# OR
print(13 > 14 or 1 > 0)

# NOT
print(not(1 < 100 and 10 < 100))
True
True
False

3.5. Identity Operators

In [99]:
x = 10
y = 100.0

# IS
print(x is y)
print(x is x)

# IS NOT
print(x is not y)

x = y
print(x is y)
print(x is not y)
False
True
True
True
False

3.6. Membership Operators

In [102]:
weekdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')

# IN
print('Thursday' in weekdays)
print('Saturday' in weekdays)

# NOT IN
print('Sunday' not in weekdays)
True
False
True

3.7.8. Bitwise Operators in Python

In [80]:
# Bitwise AND
print(17 & 18)

# Bitwise OR
print(16 | 10)

# Bitwise XOR
print(26 ^ 2)

# Bitwise NOT
print(~4)

# Bitwise signed right shift
print(24 >> 2)

# Bitwise zero fill left shift
print(12 << 2)
16
26
24
-5
6
48

3.8. Unary Operators

In [127]:
x = 5
y = -10
z = False

# Negative
print(-x)

# Unchanged
print(+y)

# Not
print(not z)

# Bitwise NOT
print(~x)
-5
-10
True
-6

Homework

Using only pen and paper, compute the output of the following Python statements

In [ ]:
# Question 1
print(47 & 55)

# Question 2
print(59 | 44)

# Question 3
print(16 ^ 12)

# Question 4
print(131 // 8)

# Question 5
print(0b1110101)

# Question 6
print((2 + 2) ** (2 + 2))

# Question 7
print(10 * 10 + 2 * (100 / 10))

# Question 8
print(11 & 13 * 2 ** 3)

# Question 9
print((1000 % 30 ^ 17 % 3 ** (1 + 1)) ** 8)

# Question 10
print((~bool(None)) ** 2 << 4)