SoFunction
Updated on 2024-11-10

The way variables and data types are used in Python

Variables and data types are very basic concepts in Python and are described in detail below:

1. Variables

Variables are containers used to store data and can be used multiple times in a program. In Python, variables must be named according to the following rules:

  • Variable names can only contain letters, numbers and underscores.
  • Variable names cannot begin with a number.
  • Variable names cannot be keywords in Python, such as if, else, for, etc.
  • Variable names should be concise, descriptive, and should not use single characters.

Variables are very easy to use, just put an equal sign in front of the variable name.

For example, here is a simple example of a variable:

message = "Hello, World!"
print(message)

The above code defines a variable called message and assigns the string "Hello, World!" to it. The value of the variable is then output using the print function.

2. Data types

In Python, common data types include numbers, strings, lists, tuples, dictionaries, and collections.

Each data type has its own specific operations and methods, here are the common data types and how they are used:

  • Number types: including integers, floating point numbers, and complex numbers. Basic mathematical operations can be performed, such as addition, subtraction, multiplication, division, and modulo.

Example:

a = 10
b = 3.14
c = 2 + 3j

print(a + b)  # Output 13.14
print(a * b)  # Output 31.4
print()  # Output 2.0
print()  # exports 3.0
  • String type: used for processing text data. Strings can be defined using single or double quotes, or triple quotes for multi-line strings.
  • You can use the + operator to splice strings and the * operator to repeat strings.

Example:

name = "John"
age = 25

message = "My name is " + name + " and I am " + str(age) + " years old."
print(message)  # exports My name is John and I am 25 years old.
  • List type: used to store a set of data.
  • Elements in a list can be of any data type, and you can use subscripts to access elements in a list, or slices to get a subset of the list.
  • You can use the append method to add elements to the list and the remove method to remove elements from the list.

Example:

fruits = ['apple', 'banana', 'orange']
print(fruits[1])  # Output banana

('pear')
print(fruits)  # Output ['apple', 'banana', 'orange', 'pear']

('banana')
print(fruits)  # exports ['apple', 'orange', 'pear']
  • Tuple type: similar to list, but elements cannot be modified.
  • You can use subscripts to access elements in a tuple, or you can use slices to get a subset of the tuple.

Example:

numbers = (1, 2, 3, 4, 5)
print(numbers[2])  # exports 3
  • Dictionary type: an unordered collection of key-value pairs. You can use the keys to access the values in the dictionary, or you can use the items method to get all the key-value pairs in the dictionary.
  • You can use the update method to add key-value pairs to the dictionary and the del statement to remove key-value pairs from the dictionary.

Example:

person = {'name': 'John', 'age': 25, 'city': 'New York'}
print(person['city'])  # Output New York

({'gender': 'male'})
print(person)  # Output {'name': 'John', 'age': 25, 'city': 'New York', 'gender': 'male'}

del person['age']
print(person)  # exports {'name': 'John', 'city': 'New York', 'gender': 'male'}
  • Set type: an unordered collection of a set of elements.
  • You can use the in keyword to determine whether an element is in the collection, or you can use the add method to add an element to the collection, or you can use the remove method to remove an element from the collection.

Example:

numbers = {1, 2, 3, 4, 5}
print(3 in numbers)  # Output True

(6)
print(numbers)  # Output {1, 2, 3, 4, 5, 6}

(4)
print(numbers)  # exports {1, 2, 3, 5, 6}

3. Data type conversion

In Python, you can use int, float, str, list, tuple, dict, and set functions to perform data type conversions.

For example, you can use the int function to convert a string to an integer, or you can use the str function to convert an integer to a string.

Example:

a = "123"
b = int(a)
c = str(b)

print(a, type(a))  # Output 123 <class 'str'>
print(b, type(b))  # Output 123 <class 'int' >
print(c, type(c))  # exports 123 <class 'str'>

It is important to note that exceptions may occur when converting data types, for example, when converting a string to an integer a ValueError exception will occur if the string contains non-numeric characters. Therefore, you need to pay attention to the exception handling when converting data types.

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.