This note is about basic Python. Data type and syntaxes. From interactivepython.org
Online Playground for Python
If you are too lazy to install Python (or Anaconda / Jupyter Notebook) in your computer, just go to: https://repl.it/ for online playground. You can start playing with any Python code right away.
No installation required. Highly recommended.
Data Structure Types in Python
- List (or Array in other languages)
- [1,2,3]
- mutable
- sequential
- Nice to have features
- x in list – return TRUE if x is in the list
- .append(x) – Insert x to last position
- .insert(i, x) – Insert x at position i
- .pop(i) – Remove value and pop from position i
- .index(x) – return index of value x
- String
- e.g. “Hello”
- immutable
- sequential
- Useful functions
- .find(x) – return index of character x
- .split(x) – Split string into list by using x as seperator
- Set (like Set in Mathematics)
- e.g. {1,2,3}
- immutable
- non-sequential
- Set features
- .union(second_set) – combine 2 sets
- .intersection(second_set) – get values common to both sets
- .difference(second_set) – get values in first set that are not in second set
- Tuples (like immutable list)
- e.g. (1,2,3)
- immutable
- sequencial
- can be used to compare, but not edit
- Dictionary
- e.g. {“key”: “value”, “key2”: “value2”}
- basically key-value list
- mutable
- non-sequential
- .get(‘key’) – to get value
Extra Type: Range (generate range of numbers)
- e.g. range(5, 10) >>> [5,6,7,8,9,10]
- range(5, 10, 2) >>> [5,7,9] (last argument 2 means +2 every time)
- range(10, 5, -2) >>> [10,8,6] (last argument -2 means -2 every time)
- immutable
String Formatting
Print function can be as advanced as printf in other language.
print("%s is %d years old." % (aName, age))
The format: "string with %d %s" % (var1, var2)
% = format operator
%s = string
%d = integer
Iterations
Python has the same syntax as other languages, but with semicolon (:) instead of brackets { }
For example:
for item in [1,2,3]: print(item)
- While Loop
- e.g. while counter <= 10 and not done:
- For Loop (in list)
- e.g. for item in [1,2,3]:
- For Loop in String works too !
- e.g. for character in string:
Conditions
- If
- e.g.
if n>4:
else:
- e.g.
Get input from user
Just use input() to open prompt window
x = input("What is your name?") print(x)
List Comprehension
Operations can be used when initializing list o__O
>>> sqlist=[x*x for x in range(1,11)] >>> sqlist [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Condition can be used:
>>> sqlist=[x*x for x in range(1,11) if x%2 != 0] >>> sqlist [1, 9, 25, 49, 81]
Import 3rd party Library
You can just type import lib_name
e.g. import math
Error/Exception Handling
It’s as simple as try: and except: block to catch error in Python.
try: print(math.sqrt(anumber)) except: print("Bad Value for square root") print("Using absolute value instead") print(math.sqrt(abs(anumber)))
or we can raise our own runtime error:
raise RuntimeError("You can't use a negative number")
Defining Functions
Creating new function use the keyword “def” which is the same as in Ruby.
def func_name(arg): return arg*2
Leave a Reply