Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

1| Page

What is Python?
Python is an interpreted, object-oriented, high level
programming language with dynamic semantics. Python is
simple, easy to learn, syntax emphasizes readability and
therefore reduces the cost of program maintenance. Python
supports modules and packages, which encourages program
modularity and code reuse.

Versions of Python
Since 1994, Python has been released in various versions, like
Python 1.x, Python 2.x, Python 3.x.

Guido Russom invents python. We generally refer to two


python versions, i.e. 2.x and 3.x. By 2020 Python 2.x is getting
over. According to PSF (Python Software Foundation) – ‚We have
decided that January 1, 2020, was the day we sunset Python 2.x.
That means we will not improve it anymore after that day, even if
someone finds a security problem in it. You should upgrade to
Python 3.x as soon as you can‛

So, it is good to practice to start with the 3.x version. The


current version of python is 3.10.2, as I am writing this.
2| Page

Primitive Types
Variables
Here you are going to learn about Variables, which are one of
the most fundamental concepts in programming. They are not
specific to python. They exist in pretty much every
programming language out there. We use variables to
temporarily store data in a computer’s memory, that we can use
later.

Here’s an example, type and run this code,

This code will print the value you assign to ‘name’ variable on
terminal.
3| Page

This is how we define variables in python3, we start with an


identifier which is the name of our variable, in our case ‘name’ is
the identifier, then an ‘=’ (equal) sign and finally the value, in our
case ‘Shahadath’ is the value. We can always update the value of
‘name’ variable in our code.

Let’s run this code and see,

In the terminal you will see two printed text, first one will be
‘Shahadath’ and the second one will be ‘Shahriar’
4| Page

Notice carefully, our code executed line by line. Let’s see what
happened here. In line 1, we assign a value to our ‘name’
variable. In line 2, we printed the value of our ‘name’ variable. In
line 3, we re-assign the value of our ‘name’ variable. In line 4, we
again printed the value of our ‘name’ variable, which is re-
assigned in line 3.

Variable Names
Now, how can we write perfect variables name so that our code
looks clean. In programming writing a clean, meaningful code is
very important. There’s some naming rules that we can follow
to make our code looks cleaner and meaningful.

A variable can have a short name (like x and y) or a more


descriptive name (name, firstName, first_name).

 A variable name must start with a letter.


 A variable name cannot start with a number.
 A variable name can only contain alpha-numeric characters
and underscores. (A-z, 0-9, and _ )
 Variable names are case-sensitive. (name, Name and
NAME are three different variables)
5| Page

These are some examples of meaningful variable name.


6| Page

Variable names with more than one word can be difficult to


read. There are several techniques you can use to make them
more readable:

camelCase,

Each word, except the first, starts with a capital letter.

PascalCase,

Each word starts with a capital letter.

Snake_Case,

Each word is separated by an underscore character.

Variable in Details
Python allows you to assign values to multiple variables in one
line.

Note: Make sure the number of variables matches the number


of values, or else you will get an error.
7| Page

And you can assign the same value to multiple variables in one
line.

If you have a collection of values in a list, tuple etc. Python


allows you to extract the values into variables. This is called
unpacking.
8| Page

The Python ‘print’ statement is often used to output variables.

To combine both text and a variable, Python uses the ‘+’ (plus)
character.

You can also use the ‘+’ (plus) character to add a variable to
another variable.
9| Page

For numbers, the ‘+’ (plus) character works as a mathematical


operator.

Note: If you try to combine a string and a number, Python will


give you an error.
10 | P a g e

Variables that are created outside of a function (as in all of the


examples above) are known as global variables. Global variables
can be used by everyone, both inside of functions and outside.

Here, in line 1, ‘name’ variable is a global variable, so that in line


4, we can use ‘name’ variable inside a function.

If you create a variable with the same name inside a function,


this variable will be local, and can only be used inside the
function. The global variable with the same name will remain as
it was, global and with the original value.
11 | P a g e

We know that, we can update a variable by re-assigning the


value of the variable. In line 4, we re-assigned the value of
‘name’ variable inside a function so it becomes a local variable to
the function. In line 5, we are using that local variable. In line 7,
we are calling the function. In line 9, we are printing the global
‘name’ variable.
12 | P a g e

Normally, when you create a variable inside a function, that


variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the


‘global’ keyword.

In line 2, we are defining a global variable inside a function by


‘global’ keyword. In line 8, we are using the global variable we
defined in a function.
13 | P a g e

Also, use the ‘global’ keyword if you want to change a global


variable inside a function.

In line 4, we are defining ‘global’ keyword to use the global


variable. In line 5, we are updating the value of global ‘name’
variable inside a function. In line 10, we are printing the updated
‘name’ variable.

You might also like