Welcome To Python!
Welcome To Python!
Welcome To Python!
Welcome to Python!
Welcome to Python!
The three major versions of Python are 1.x, 2.x and 3.x. These are
subdivided into minor versions, such as 2.7 and 3.3.
Code written for Python 3.x is guaranteed to work in all future versions.
Both Python Version 2.x and 3.x are used currently.
This course covers Python 3.x, but it isn't hard to change from one
version to another.
Python has several different implementations, written in various
languages.
The version used in this course, CPython, is the most popular by far.
An interpreter is a program that runs scripts written in an interpreted
language such as Python.
In this lesson, we will start using Python, via the Python console. The first
step is to download Python from www.python.org.
You should download the Python 3.x version that is compatible with your
operating system. Once installed, the Python console can be accessed
using one of several ways, including using the command line, running the
Python interpreter directly, or running a GUI that comes installed with
Python called IDLE.
We'll use all of these ways during this course, but we'll start with IDLE, as
it is the easiest.
After running IDLE, you should see a prompt of three right arrows.
Type in "print('Hello world!')" and press enter. You should see the
following:
>>> print('Hello world!')
Hello world!
Simple Operations
Python has the capability of carrying out calculations.
Enter a calculation directly into the Python console, and it will output the
answer.
>>> 2 + 2
4
>>> 5 + 4 - 3
6
The spaces around the plus and minus signs here are optional (the code
would work without them), but they make it easier to read.
Simple Operations
Simple Operations
The minus sign indicates a negative number.
Operations are performed on negative numbers, just as they are on
positive ones.
>>> -7
-7
>>> (-7 + 2) * (-4)
20
The plus signs can also be put in front of numbers, but this has no effect,
and is mostly used to emphasize that a number is positive to increase
readability of code.
Simple Operations
Dividing by zero in Python produces an error, as no answer can be
calculated.
>>> 11 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
In Python, the last line of an error message indicates the error's type.
Read error messages carefully, as they often tell you how to fix a
program!
Floats
Floats are used in Python to represent numbers that aren't integers.
Some examples of numbers that are represented as floats are 0.5 and
-7.8237591.
They can be created directly by entering a number with a decimal point,
Floats
As you saw previously, dividing any two integers produces a float.
A float is also produced by running an operation on two floats, or on
a float and an integer.>>> 8 / 2
4.0
>>> 6 * 7.0
42.0
>>> 4 + 1.65
5.65
A float can be added to an integer, because Python silently converts
the integer to a float. However, this implicit conversion is
the exception rather the rule in Python - usually you have to convert
values manually if you want to operate on them.
Exponentiation
Besides addition, subtraction, multiplication, and division, Python also
supports exponentiation, which is the raising of one number to the
power of another. This operation is performed using two asterisks.
>>> 2**5
32
>>> 9 ** (1/2)
3.0
Newlines
Python provides an easy way to avoid manually writing "\n" to escape
newlines in a string. Create a string with three sets of quotes, and
newlines that are created by pressing Enter are automatically escaped for
you.
>>> """Customer: Good morning.
Owner: Good morning, Sir. Welcome to the National Cheese Emporium."""
'Customer: Good morning.\nOwner: Good morning, Sir. Welcome to the
National Cheese Emporium.'
As you can see, the \n was automatically put in the output, where we
pressed Enter.
Output
Usually, programs take input and process it to produce output.
In Python, you can use the print function to produce output. This displays
a textual representation of something to the screen.
>>> print(1 + 1)
2
>>> print("Hello\nWorld!")
Hello
World!
When a string is printed, the quotes around it are not displayed.
Input
To get input from the user in Python, you can use the intuitively
named input function.
The function prompts the user for input, and returns what they enter as
a string (with the contents automatically escaped).
>>> s = input("enter number: ")
enter number: 123
>>> print(s)
123
Concatenation
As with integers and floats, strings in Python can be added, using a
process called concatenation, which can be done on any two strings.
When concatenating strings, it doesn't matter whether they've been
created with single or double quotes.
>>> "Spam" + 'eggs'
'Spameggs'
>>> print("First string" + ", " + "second string")
First string, second string
Concatenation
Even if your strings contain numbers, they are still added as strings rather
than integers. Adding a string to a number produces an error, as even
though they might look similar, they are two different entities.
>>> "2" + "2"
'22'
>>> 1 + '2' + 3 + '4'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In future lessons, only the final line of error messages will be displayed, as it is
the only one that gives details about the type of error that has occurred.
String Operations
Strings can also be multiplied by integers. This produces a repeated
version of the original string. The order of the string and
the integer doesn't matter, but the string usually comes first.
Strings can't be multiplied by other strings. Strings also can't be multiplied
by floats, even if the floats are whole numbers.
>>> print("spam" * 3)
spamspamspam
>>> 4 * '2'
'2222'
>>> '17' * '87'
TypeError: can't multiply sequence by non-int of type 'str'
>>> 'pythonisfun' * 7.0
TypeError: can't multiply sequence by non-int of type 'float'