Variable in Python

Tram Ho

Like the previous post, I went through the basics of python and ran the first program, the eternal program of Hello world. If you have not seen it, see here . For this part, I will go over the variable in python. Let’s go.

Variable naming rules

First, let’s go over the variable naming rules. Whatever you do, you must know its rules.

Rule:

  • Variable names can only contain letters, numbers and an underscore _. Can start with a letter or an underscore, but cannot begin with a number. For example: bien , _bien but can not be set as 1_bien .
  • Spaces are not allowed when naming variables, but you can use underscores to separate words.
  • When naming a variable, do not use Python keyword and function name. Keywords in python that we cannot set for variables.

Data types in python

The 5 standard data types in python are:

  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary

We can use the type () function to check the data type of the variable

Number

Python supports 5 different types of numbers data:

  • Int
  • Float
  • Long
  • Complex (complex numbers)
  • Fraction (decimal)

Basic operations with the type number: +, -, /, *,%, //, **

String

Besides numbers, Python can also manipulate strings, which are represented in many ways. They can be enclosed in single quotes (‘…’) or double (“…”) (“” “…” “”) with the same result. Strings can be indexed with the sequence number of the character in the string.

word = "Python"

word[0] => P

word[0:2] => "Py"

word[2:5] => "tho"

String concatenation uses the + operator.

List

In Python, list is represented by an array of comma separated values ​​enclosed in []. Lists can contain multiple items of different types, but are usually items of the same type (which looks like an array, but differs from an array in that the elements can be of different type). squares = [1, 4, 9, 16, 25]

list = [1, "Hello", 9.6]

Index (index) of the list: to access the elements of the list we use the index, the order of the elements in the list to retrieve. eg:

Other methods:

Since the types of data in other programming languages ​​exist, I will write it in the shortest possible way. There are two data types below, which are only available in python.

Tuple

Tuple in Python is a sequence of elements with the same order as list. The difference between list and tuple is that we cannot change elements in tuple when assigned, but in list elements can change. Tuple is often used for data that does not allow modification.

Tuples are often used for elements of the same (different) data type, and lists are often used for elements of the same data type. The Tuple does not change when in use, so when using it will ensure that data is not changed.

Tuple is created by enclosing all of its elements in parentheses (), separated by commas. You can remove the parentheses if desired, but should add it to the code more clearly.

Tuple has no limit on the number of elements and can have many different data types such as integer, decimal, list, string, …

Creating a tuple with only one element is a bit complicated, if you create a tuple in the usual way that it is not enough to enclose the element in (), a comma is needed to indicate that this is a tuple.

To access each element in a tuple is similar to a list using []:

Unlike lists, tuple cannot be changed. This means that elements of a tuple cannot be changed once assigned. But, if the element itself is a mutable data type (such as a list), nested elements can be changed.

The elements in the tuple cannot be changed, so we cannot delete or remove elements from the tuple. But completely deleting a tuple is possible with the del keyword.


The conclusion is quite long, about the variable type in python, there are two more types: set and dictionary, I would like to leave the number below.

Share the news now

Source : Viblo