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
In this module, we will cover the fundamental building blocks of the Python programming language, namely:
# Logical line spanning a single physical line
my_string = 'My entire string on a single physical line.'
print(my_string)
# 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)
# 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")
# Implicit line joining
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', # Weekdays
'Saturday', 'Sunday') # Weekend
print(days)
# Multiple logical lines spanning a single physical line
from datetime import date; today = date.today(); print(today)
# 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")
# 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")
# Valid identifiers
my_first_number = 100
my_first_string = 'Hello World'
# Invalid identifiers
1number = 10
string-test = 'Invalid Identifier'
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)
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)
# 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)
# 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)
# 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)
x = None
print(x)
if x:
print('x is True')
else:
print('x is not True')
print(bool(None))
# 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)
# 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)
# 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)
# AND
print(1 < 100 and 10 < 100)
# OR
print(13 > 14 or 1 > 0)
# NOT
print(not(1 < 100 and 10 < 100))
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)
weekdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
# IN
print('Thursday' in weekdays)
print('Saturday' in weekdays)
# NOT IN
print('Sunday' not in weekdays)
# 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)
x = 5
y = -10
z = False
# Negative
print(-x)
# Unchanged
print(+y)
# Not
print(not z)
# Bitwise NOT
print(~x)
# 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)