Getting Started With Python
These are my notes on getting started with Python.
Python programming books on Amazon
Introduction
Python is one of the most popular programming languages. It is used for a lot of different fields including, science, data analysis, and application development.
Its commands are processed by an interpreter.
Most of Python’s keywords are English words that make it easy for beginners to understand. It uses indentation liberally to group statements into blocks of code. This helps in reading and comprehending the code.
Installing Python
For the Windows operating system, you just go to Python’s website and download the executable.
Follow the instructions and you will soon have a functional Python interpreter installed and working.
For Linux systems, Python usually comes with the distribution. So, there will usually be nothing to do except update your version. To update your version, just use your package manager to do this. Linux users will understand what this means immediately. If it is not installed, you can use your package manager to install it. Search python3 to find it.
Using The Interpreter
The Python interpreter processes text. You type this text in the shell of your operating system or in a text editor.
You can interact with the Python interpreter a few different ways.
- In Windows, open CMD and type python
- In Linux, open a terminal window and type python
- From the start menu, you can choose Python
- From the start menu, you can choose the IDLE program
You can use the interpreter as a calculator with parentheses as needed.
Your First Program
You can type commands and write your first program from this window. A Python program is just a text file with commands in it and saved with the .py extension. It is also a good idea to create a python folder somewhere to save your programs.
In your interpreter window, we can print some text. That is technically a program but it is what most languages start with. Type:
print(‘I am learning Python’)
Use the menu to save this to your computer. Call it something like ‘learn.py’. Name it whatever you want though, it does not matter.
You can navigate to the file through your gui and double click it.
You can also navigate to it through your command line. If you do it this way, once you are in the directory where the file is located, just type ‘python learn.py’ to run it.
Using Variables
All programming languages have variables. Python is no exception. Variables let you store data. You can then run operations on the variables as needed. Name your variables something meaningful.
life_counter=0
temperature=55
The first part is the variable name followed by an equals sign. After that contains the value of the variable. All variables must be initialized when created. This means they must be assigned a value when they are first created.
You do not have to specify a data type when creating variables. This is called dynamic typing.
Comments
Comments are a universal way for programming languages to document code. They are meant to explain the thoughts behind a statement, the purpose of a block of code, and to make it easier on anyone reading the code at a later date.
You use the # symbol in front of a line of text to make it a comment.
# The purpose of this variable is to be a counter and keep track of the iterations in a loop
You can do multi-line comments by enclosing text within triple quote marks(“””).
User Input
To capture user input, we use the input() function. It will accept a string inside its parentheses. Afterwards, it will prompt the user for input. User input is read as a text string. The text string is then assigned to a variable.
fav_team=input(‘What is your favorite baseball team? : ‘)
print(fav_team)
Errors
There are three types of errors that can occur. They are:
- Syntax
- Runtime
- Semantic
Syntax errors are when you use the Python language incorrectly. An example of this is misspelling a keyword. Runtime errors can happen when you run a program. This can be a lot of different things but generally means something was misused. Semantic errors are not technically errors. The program can still run but you get unexpected results.
Overview
The Python language is processed by the interpreter. It is a high level language because it uses English wording. Indentation is used to group statements into blocks. A Python program is just a text file that uses the .py file extension. The print() function outputs the string enclosed within its parentheses. String values are enclosed within quote marks.
A variable is a container with a name. It stores values and can be used by referencing that variable’s name. Variables can be any data type but they all have to be initialized first with some value. The input() function accepts user input. You can assign this input to a variable to increase your program usefulness.