Python Overview
Python is a modern programming language useful in various fields of computer science including teaching, maths, data science, machine learning, so-called artificial intelligence, graphics, desktop application development, and cloud application development.
Language Features
- Builtins are automatically imported
- Unpacking: first, second, third, fourth = astring
- Swapping: x, y = y, x
- Method parameters are passed by reference (id is the same)
Style Guide
- Four space tabs
- Two lines after a function
- Variable names with no capital letters
- Function names with no capital letters
IDLE
Provided with the Python distribution. Generally only used in the early days of teaching Python. Useful for quickly checking Python statement syntax, and for running quick calculations requiring python libraries like Math and Pandas.
PyCharm
Preferred tool by certain tutors for teaching software engineering with Python. Also supports HTML, Jupyter Notebooks, JavaScript, TypeScript and SQL.
Show underlying function code: Right click > Go To > Declarations or Usages (F12)
Strings
C style placeholders
integer_value = 4237 print("Decimal integer %d more text" % integer_value) print("Hexadecimal integer %x\n" % integer_value) print("Hexadecimal integer %X\n" % integer_value) print("Right justify integer (%8d)" % integer_value) print("Left justify integer (%-8d)\n" % integer_value)
f-Strings
print(f"Hello {fn} {ln} you are {age} years old!")
Splitting Strings
>>> my_string = "Shannon" >>> my_letters = list(my_string) ['S', 'h', 'a', 'n', 'n', 'o', 'n']
Tuples
The standard way to create tuples is with round brackets. They can also be created without. They can also be created with a single value.
standard_tuple = (1, 2, 3, 4) no_brackets_tuple = 5, 6, 7, 8 single_value_tuple = 9,
List Iteration
names = ["Baker", "Patterson", "Greening", 2547, 99.442] names.append("Brown") names.insert(0, 'Douglas') for lcv in range(len(names)): print(f'{lcv:4} {names[lcv]:15}')
lcv stands for loop control value.
To use the += operator to add a value to a list, the second value must come from another list
>>> list += number TypeError: 'int' object is not iterable >>> list += [number]
List from Range
a_range = range(0, 199, 2) a_list = list(a_range) print("Type of a_range:", type(a_range), "\n", a_range) print("Type of a_list:", type(a_list), "\n", a_list) for number in a_list: print(number, end=" ")
Type Comparision
type(9) is int type(2.5) is float type('x') is str type(u'x') is unicode type(2+3j) is complex type(my_tuple) is tuple variable is None isinstance( 'x', basestring ) isinstance( u'u', basestring ) isinstance( 9, int ) isinstance( 2.5, float ) isinstance( (2+3j), complex )
Some Important Methods
The Python interpreter has a number of functions and types built into it that are always available: Built-in Functions. Some selected examples are as follows...
print("Hello", "World", sep="_", end="!") die = random.randrange(1, 7) integer = int(input("Enter and integer: ")) print("The integer is:", integer, "; the id is:", id(integer), "; the type is:", type(integer)) del my_dictionary["Dict-Key"] my_list.count(value) type(my_list) id(my_list)