Among programming languages, one of the easiest to learn for beginners is Python due to its syntax and versatility. It has a wide array of applications including but not limited to, web development, automation, artificial intelligence, and data science. If you are new in learning Python language, then basics should be demonstrated as they are the building blocks. Most of the python programs are based on basic concepts of variables, data types and operators. In this article we'll look into these concepts more closely.

What Questions Do You Have Regarding Variables?

A variable is a reserved memory location to store values. In simpler terms, a variable is a name that is used to identify a particular memory location in a computer. Any programming language uses the concept of a variable to enable a program to remember and change information.

In Python, creating a variable is done by just naming it and assigning a value to it with the assignment operator (`=`). It is also good practice with variable names that are descriptive enough which will help other users or you in the future to maintain the code.

Let's say that you wanted to record the age of a person. Then you would have a variable such as:

Assign Variable

  age = 25
In this case, The variable is called `age` which contains the number `25`. Being a dynamically typed language, python does not require one to declare the variable type first. It does so on its own by looking at whatever is assigned to the variable. Here, the language interprets that 'twenty-five' is an integer.

The Different Data Types in Python

Each and every variable in python has a particular data type. A data type is a classification of a variable according to the kind of data it can hold as well as the operations that can be performed on it. The data types in Python are numerous, with every single one of them having its own set of specific restrictions on what can be done in the language.

1. Integers (`int`)

As the term suggests, an integer is a whole number that can be either positive or negative but does not contain any decimals. One of the most common uses of integers is for counting, indexing, or general mathematics.

Example:

Integer Variable

  count = 10
In this case, `count` is declared to be an integer variable with a value of `10`.

2. Fear of the Bigger Picture Is Obliterated: Float

Float means the numbers which have a dot or decimal point. The calculations which have to do with currency or some scientific measurements and require accuracy use floating-point values. To illustrate:

Float Variable

  height = 5.9
So in this case `height `is a float variable which captures `5.9`.

3. Specials Variable-types/pointer (str)

A string is done by combining characters. These characters can be enclosed inside single or double quotations. A string is a pointer type variable which is by default used for text-oriented data i.e. name, address, description, etc. To illustrate:

String Variable

  name = "Alice"
In this instance `name` acts as a string variable and the output text is `Alice`.

4. Cash is a Boolean peddlerous

In some instances cash becomes a true or false binary. Therefore, a boolean becomes a data type that can only take on two values, either true or false. In our programs, booleans help run the conditions which determine which areas of the particular program will execute. To illustrate:

Boolean Variable

  is_sunny = True
In this instance, `is_sunny` is a boolean variable where `True` is the value which has been assigned.

5. Lists

A list is an assortment of items of any data type. Lists have a defined order or sequence which allows each element the ability to be accessible by its position. Square brackets `[]` are used to create lists, placing each item in the list in an order that is separated by commas.

Example:

List Variable

  fruits = ["apple", "banana", "cherry"]
So in this example, a variable `fruits` represents the list of three string items.

6. Dictionaries (`dict`)

A dictionary consists of a disoriented collection of pairs referred to as key and value. Such that each key is distinct and to each key a value can be attached and retrieved with using the key. The structure described by this form uses the following symbols: {}.

Example:

Dictionary Variable

  person = {"name": "Alice", "age": 25} 
Here in the example `person` is a dictionary consisting of two key derivatives and their values: `name`/`Alice` and `age`/`25`.

7. Tuples

A tuple can be simply put as a read only list, which means once it has been created, its values can never be changed. Tuples come in handy in situations where the data should necessarily remain unchanged.

Example:

Tuple Variable

  coordinates = (10, 20) 
So here in the defined case of example `coordinates` a pair of numbers i.e. `10` and `20` forms a tuple.

Operators in Python

An operator is a procedure which operates on variables or values. It is concerned with handling data and carrying out various functions, including computations, comparison, and logic.

1. Arithmetic Operators

Mathematical operations are carried out using arithmetical operators - such as add, subtract and multiply, as well as divide.

Below are the common arithmetic operators found in Python:

Arithmetic Operators

  +   (Addition)
– (Subtraction)
(Multiplication)
/ (Division)
// (Floor Division)
% (Modulus)
(Exponentiation)
For Example,

Arithmetic Operators

  a = 5
b = 3
print(a + b) # Output: 8

2. Comparison Operators

A comparison operator is applied to two values to determine whether they are equal, greater than or less than each other. When a comparison operator is used, a boolean value is returned. This value can only be of two types: True or False. The values returned depend on the outcome of said comparison. Comparison operators that are commonly used include but are not limited to:

Arithmetic Operators

  == (Equal to)
!= (Not equal to)
> (Greater than)
< (Less than)
> = (Greater than or equal to)
<= (Less than or equal to)
For example

Comparison Operators

  x = 10
y = 5
print(x > y) # Output: True

3. Logical Operators

Logical operators are used to combine two or more expressions and return to the user if the specified multiple conditions are satisfied or falsified so as to dictate the flow of a program; they provide a practical approach to evaluating the truthfulness of multi-conditional statements. The logical operators in Python includes but are not limited to:

```
and (Will return `True` only if both conditions are true)
or (Will return `True` if at least one of the conditions is true)
not (Will convert a `True` into `False` and vice-versa)
```

For example,

Logical Operators

  print(x < 10 and y > 5)   # Output: True 

4. Assignment Operators

To give variables values, assignment operators are used. Commonly it is written with the symbol ′=′ as a matter of fact Python supports compound assignment operators too. These are like, an initial enhancement does a mathematical calculation along with assignment, such as:

- "+=" (Add)
- "-=" (Subtract)
- "=" (Multiply)
- "/= (Divide)

Example:

Assignment Operators

  x=5
x +=3 # Creates the value of x at 8

5. Membership and Identity Operators

However when dealing with the sequences or the sets of items, there are also operators that help you check for membership or identity. Different kinds of them are,

- "in" (It checks if the member is present in the given list or other sequences like string)
- "is" (It checks whether 2 variables refer to the same value in the memory block).

Example:

Membership Operator

  fruits = [ "apple", "banana", "cherry"]
print( "banana" in fruits ) # Output: true
Relate Articles…