Learning Basic Python Amlan Talukder
Why Python? Easier language to code Can get things done faster in less effort Easier language to learn Platform independent
How to install Python (Windows/Mac) https://www.python.org/downloads/
How to install Python (Linux) sudo apt-get install python (Debian/Ubuntu) sudo yum install python (RedHat/CentOS/Fedora)
Useful python resources Online tutorial http://www.learnpython.org/ Book Dive into Python
How to run Python Open “Command Prompt” in Windows OS or “Terminal” in Linux/MAC OS Type the following command to run a code file named “mypython.py”
How to run Python Open a program named “IDLE (Python GUI)” Open your python source file “mypython.py” in IDLE Type print ‘Hello World’ in the editor Click “Run -> Run Module” to run the code
Python variables No need to declare the type of a variable Any variable type can be converted into another type Variable Types: int (Any integer, ex: 0, 12, 29, 998) float (Any floating point number, ex: 12.3652) str (Any text, ex: ‘The sky is blue’ or “The quick brown fox”) bool (True or False) For single line comment, use ‘#’ and for multiline comment, use ”””
Python variables
Condition statements if <statement>: <do something> else: <do something else> if <statement>: <do something> elif <another statement>: <do something else> else:
Loops in Python
Functions in Python
List List is a collection of objects (variables or other lists or dictionaries)
List functions A[x], get element in list A at position x A.append(x), append x to A list A.insert(p,x), insert x to A at position p A. index(x), get the first position of x in A, error it not found A.count(x), get the occurrence of x in A A.sort(reverse=True/False), sort list A. True/False set the sorting order. A[x:y] , get the subset of list A from position x to position y.
String functions
String functions A[x], get the element at position x of A len(A), get the length of string A A.upper(), get all uppercase of string A A.lower(), get all lowercase of string A A.split(), split string A with specific separator. .join(Asplit), join the list of strings Asplit with a separator A[x:y], the substring of A from x to y
List comprehension
File read/write
Basic operators and “math” library +, -, *, /, **, +=, -=, %, and, or, =, == math.sqrt(number) math.pow(number, power) math.log(number) math.ceil(number) math.floor(number)
Basic operators and “math” library
Questions?