Python Notes [N°1] - Getting Started
Hello Data Lovers👋
Another article for Pythonic, Pythonist, Pythoneer and Pythonista. Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991.
Verify if Python is installed
Hello World in Python
Create your first variable
Rules for variable naming
Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991. Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, functional programming, and procedural styles. It has a large and comprehensive standard library.
Two major versions of Python are currently in active use:
Python 3.x is the current version and is under active development
Python 2.x is the legacy version and will receive only security updates until 2020.
PAY ATTENTION -> No new features will be implemented. Note that many projects still use Python 2, although migrating to Python 3 is getting easier
Verify if Python is installed
To confirm that Python was installed correctly, you can verify that by running the following command in your favorite terminal (If you are using Windows OS, you need to add the path of python to the environment variable before using it in the command prompt):
$ python — — version
If you have Python 3 installed, and it is your default version you should see something like this:
$ python — version Python 3.6.0
If you have installed Python 3, but $ python — version outputs a Python 2 version, you also have Python 2 installed. This is often the case on macOS and many Linux distributions.
Hello World in Python using IDLE
IDLE is a simple editor for Python, that comes bundled with Python.
Open IDLE on your system of choice
It will open a shell with options along the top
In the shell, there is a prompt of three right angle brackets: something like this:
Now write the following code in the prompt:
print("Hello, World") Hello, World
Create your first variable
To create a variable in Python, all you need to do is specify the variable name, and then assign a value to it.
<variable name> = <value>
Python uses = to assign values to variables.
There’s no need to declare a variable in advance (or to assign a data type to it), assigning a value to a variable itself declares and initializes the variable with that value. There’s no way to declare a variable without assigning it an initial value.
# Integer a = 2 print(a) 2 # Integer b = 9223372036854775807 print(b) 9223372036854775807 # Floating point pi = 3.14 print(pi) 3.14 # String c = 'A' print(c) A # String name = 'John Doe' print(name) John Doe # Boolean q = True print(q) True # Empty value or null data type x = None print(x) None
Variable assignment works from left to right. So the following will give you a syntax error.
0 = x => Output: SyntaxError: can't assign to literal
You can not use python’s keywords as a valid variable name. You can see the list of keyword by:
import keyword print(keyword.kwlist)
Rules for variable naming
- Variables names must start with a letter or an underscore
x = True # valid _y = True # valid 9x = False # starts with numeral => SyntaxError: invalid syntax $y = False # starts with symbol => SyntaxError: invalid syntax
- The remainder of your variable name may consist of letters, numbers, and underscores
has_0_in_it = "Still Valid"
- Names are case sensitive
x = 9 y = X*5 =>NameError: name 'X' is not defined
Even though there’s no need to specify a data type when declaring a variable in Python, while allocating the necessary area in memory for the variable, the Python interpreter automatically picks the most suitable built-in type for it:
a = 2 print(type(a)) <type 'int'> b = 9223372036854775807 print(type(b)) <type 'int'> pi = 3.14 print(type(pi)) <type 'float'> c = 'A' print(type(c)) <type 'str'> name = 'John Doe' print(type(name)) <type 'str'> q = True print(type(q)) <type 'bool'> x = None print(type(x)) <type 'NoneType'>
Now you know the basics of the assignment, let’s get this subtlety about the assignment in python out of the way. When you use = to do an assignment operation, what’s on the left of = is a name for the object on the right. Finally, what = does is assign the reference of the object on the right to the name on the left.
a_name = an_object # "a_name" is now a name for the reference to the object "an_object"
You can assign multiple values to multiple variables in one line. Note that there must be the same number of arguments on the right and left sides of the = operator:
a, b, c = 1, 2, 3 print(a, b, c) 1 2 3 a, b, c = 1, 2 => Traceback (most recent call last): => File "name.py", line N, in <module> => a, b, c = 1, 2 => ValueError: need more than 2 values to unpack a, b = 1, 2, 3 => Traceback (most recent call last): => File "name.py", line N, in <module> => a, b = 1, 2, 3 => ValueError: too many values to unpack
The error in the last example can be obviated by assigning the remaining values to an equal number of arbitrary variables. This dummy variable can have any name, but it is conventional to use the underscore (_) for assigning unwanted values:
a, b, _ = 1, 2, 3 print(a, b) 1, 2
Note that the number of _ and the number of remaining values must be equal. Otherwise ‘too many values to unpack error’ is thrown as above:
a, b, _ = 1,2,3,4 =>Traceback (most recent call last): =>File “name.py”, line N, in <module> =>a, b, _ = 1,2,3,4 =>ValueError: too many values to unpack (expected 3)
You can also assign a single value to several variables simultaneously.
a = b = c = 1 print(a, b, c) 1 1 1
When using such a cascading assignment, it is important to note that all three variables a, b and c refer to the same object in memory, an int object with the value of 1. In other words, a, b and c are three different names given to the same int object.
Assigning a different object to one of them afterward doesn’t change the others, just as expected:
a = b = c = 1 # all three names a, b and c refer to same int object with value 1 print(a, b, c) 1 1 1 b = 2 # b now refers to another int object, one with a value of 2 print(a, b, c) 1 2 1 # so the output is as expected
The above is also true for mutable types (like list, dict, etc.) just as it is true for immutable types (like int, string, tuple, etc.):
x = y = [7, 8, 9] # x and y refer to the same list object just created, [7, 8, 9] x = [13, 8, 9] # x now refers to a different list object just created, [13, 8, 9] print(y) # y still refers to the list it was first assigned [7, 8, 9]
So far so good. Things are a bit different when it comes to modifying the object (in contrast to assigning the name to a different object, which we did above) when the cascading assignment is used for mutable types.
Take a look below, and you will see it first hand:
x = y = [7, 8, 9] # x and y are two different names for the same list object just created, [7,8, 9] x = 13 # we are updating the value of the list [7, 8, 9] through one of its names, x in this case print(y) # printing the value of the list using its other name [13, 8, 9] # hence, naturally the change is reflected
Nested lists are also valid in python. This means that a list can contain another list as an element.
x = [1, 2, [3, 4, 5], 6, 7] # this is nested list print x [3, 4, 5] print x 4
Lastly, variables in Python do not have to stay the same type as which they were first defined — you can simply use = to assign a new value to a variable, even if that value is of a different type.
a = 2 print(a) 2 a = "New value" print(a) New value
If this bothers you, think about the fact that what’s on the left of = is just a name for an object. First, you call the int object with value 2 a, then you change your mind and decide to give the name a to a string object, having value ‘New value’.
Thanks for reading! If it was useful to you, please Like/Share so that, it reaches others as well.
To get e-mail notification on my latest posts, please subscribe to my blog by hitting the Subscribe button at the top of the page.
In the next article we will analyze: ✅ Datatypes