What is Python?
Python is a popular programming language. It was created in 1991 by Guido van Rossum.
It is used for:
- web development (server-side),
- software development,
- mathematics,
- system scripting.
Python is a popular programming language. It was created in 1991 by Guido van Rossum.
It is used for:
Python can be used on a server to create web applications.
Python can be used on a server to create web applications.
Python can be used on a server to create web applications.
Python can be used on a server to create web applications.
Python can be used on a server to create web applications.
Many PCs and Macs will have python already installed.
To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):
C:\Users\Your Name>python --version
To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:
python --version
If you find that you do not have python installed on your computer, then you can download it for free from the following website: https://www.python.org/
Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.
The way to run a python file is like this on the command line:
C:\Users\Your Name>python helloworld.py
Where "helloworld.py" is the name of your python file.
Let's write our first Python file, called helloworld.py, which can be done in any text editor.
print("Hello, World!")
Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:
C:\Users\Your Name>python helloworld.py
The output should read:
Hello, World!
Congratulations, you have written and executed your first Python program.
Python syntax can be executed by writing directly in the Command Line:
>>> print("Hello, World!")
Hello, World!
Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:
C:\Users\Your Name>python myfile.py
Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.
Python uses indentation to indicate a block of code.
if 5 > 2:
print("Five is greater than two!")
Python will give you an error if you skip the indentation:
if 5 > 2:
print("Five is greater than two!")
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type and can even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:
There are three numeric types in Python:
Variables of numeric types are created when you assign a value to them:
x = 1 # int
y = 2.8 # float
z = 1j # complex
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Complex numbers are written with a "j" as the imaginary part:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
String literals in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
Strings can be output to screen using the print function. For example: print("hello").
Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
Substring. Get the characters from position 2 to position 5 (not included):
a = "Hello, World!"
print(a[1])
The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
The len() method returns the length of a string:
a = "Hello, World!"
print(len(a))