Introduction to Python
Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming.
Some of the outstanding features of Python include:
- Clear, easy-to-read syntax.
- Object-oriented.
- Fully modular, supports hierarchical packages.
- Lots of standard libraries and extensive external modules.
- Exception-based error handling.
- High-level dynamic data type, data type interpolation
- External modules can be written in C, C++, Jython …
- Machine learning, statistical computing, data analysis.
- Powerful and fast.
Python Resources:
- Learn Python Treehouse: https://teamtreehouse.com/learn-to-code/python
- Learn Python The Hard Way: https://learnpythonthehardway.org
- Learn Python Code Mentor: https://www.codementor.io/learn-python-online
- Visualize Python: https://www.pythontutor.com
- Learn Python Code Academy: https://www.codecademy.com/learn/python
- Python Tutorial: https://www.w3schools.com/python
- Real Python: https://realpython.com
- r/Python
- And Google, or course…
Ebooks:
- Fundamentals of Python Programming – Richard L. Halterman
- Learn Python 3 the hard way – Zed A. Shaw
- Programming in Python 3 – Mark Summerfield
- Python Algorithms – Magnus Lie Hetland
- …
Programming tools:
- Notepad, Notepad++
- IDLE
- Wing Python IDE
- Pycharm (recommended)
Download Python: https://www.python.org/downloads
Python install steps:
These steps assume that you’re a Windows user, however, if you’re using Linux or Mac, then there should be plenty of how-to guides on internet, and by the way the book Learn Python 3 the hard way has very detailed setup introduction for each platform.
Check if Python is installed properly:
Go to command line by typing cmd in Windows Search (Windows key + R), then at the command prompt, type python. If it’s shown like below, then you have installed Python correctly.
Create a Python project in Pycharm
Pycharm Community Edition is enough for beginners and advance users alike to learn and work with Python. You could learn more about what is the difference between Community and Professional Edition by reading this article.
When you start Pycharm, you should see this window. Click Create New Project.
Location: Specify where you want to save your Python project.
Press small arrow before Python Interpreter to show the interpreter configuration. Choose the second option here, then press [ … ] to go to next step. At Add Python Interpreter window, pick System Interpreter option, then press [ … ] button to find path to python.exe (it should look like my path in the above image, replace with <your_username> between C:\Users\… and …\AppData\Local…)
To create new Python file, right click your project folder on the sidebar, then go to New -> choose Python File.
Now you have a blank Python file. Let’s try a few simple code. Don’t worry about these code, I will go back to explain how it works at later articles.
Then, click Run button, or press Shift + F10.
You can see result at the console below, by looking at Run tab:
And that’s it. We could prepare for next stage now.
Basic data types in Python
int : represent integers include zero, positive or negative whole numbers without a fractional part and having unlimited precision. For example: 124, -115
float : floating point numbers are positive and negative real numbers with a fractional part denoted by the decimal symbol . or the scientific notation E or e, for example: 1834.59, 3.142, -4.45, 0.713, 1.2e6, 8e-3
complex : complex number is a number with real and imaginary components. For example, 8 + 3j is a complex number where 8 is the real component and 3 multiplied by j is an imaginary component. Note: You must use j or J as imaginary component, as sing other character the program will throw a syntax error. For complex number, instead of typing a+bj, you could also use complex(a, b) function.
# example with a = 2; b = 3 z = complex(2, 3) print("Real part =", z.real) print("Imaginary part =", z.imag) # Return: Real part = 2.0, Imaginary part = 3.0
str : the sequence of Unicode characters wrapped inside single, double, or triple quotes. ‘Python’ ; “Python” ; ”’Python”’ or “””Python”””.
bool: Boolean type, store as True or False.
Declare variables in Python
In Python, a variable does not need to declare a data type, but when we assign a value to it, Python will automatically interpolate the data type of the variable. Thus, a variable can have many data types depending on the assigned value. We can use the type() function to check the data type of a variable. (different from other language like C, Java…)
A variable need to begin with a character, so a1 will work, but 1a will not.
Delete variables
If an existing variable is deleted, it cannot be used anymore. Using statement del x (with x is a variable) to delete.
Check storage area
We can check the value storage area of int and float variables by importing the sys library to see the details.
import sys print("int info: ") print(sys.int_info) print("float info: ") print(sys.float_info) # result: # int info: # sys.int_info(bits_per_digit=30, sizeof_digit=4) # float info: # sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
Continue to part 2.
[…] check part 1 first and then come back to read this article […]