Python căn bản (4): Variables (cont)

  Bài trước: Python căn bản (3): Variables

-----

[Từ điển]

4. Python Variables (cont)

4.1 Assign Multiple Values

Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line:

Example

x, y, z = "Orange", "Banana", "Cherry"

print(x)

print(y)

print(z)

Note: Make sure the number of variables matches the number of values, or else you will get an error.

One Value to Multiple Variables

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

Example

x = y = z = "Orange"

print(x)

print(y)

print(z)

Unpack a Collection

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.

Example

Unpack a list:

fruits = ["apple", "banana", "cherry"]

x, y, z = fruits

print(x)

print(y)

print(z)

4.2 Output Variables

The Python print() function is often used to output variables.

Example

x = "Python is awesome"

print(x)

In the print() function, you output multiple variables, separated by a comma:

Example

x = "Python"

y = "is"

z = "awesome"

print(x, y, z)

You can also use the + operator to output multiple variables:

Example

x = "Python "

y = "is "

z = "awesome"

print(x + y + z)

Notice the space character after "Python " and "is ", without them the result would be "Pythonisawesome".

For numbers, the + character works as a mathematical operator:

Example

x = 5

y = 10

print(x + y)

In the print() function, when you try to combine a string and a number with the + operator, Python will give you an error:

Example

x = 5

y = "John"

print(x + y)

The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types:

Example

x = 5

y = "John"

print(x, y)

4.3 Global Variables

Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

Example

Create a variable outside of a function, and use it inside the function

x = "awesome"

 

def myfunc():

  print("Python is " + x)

 

myfunc()

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.

Example

Create a variable inside a function, with the same name as the global variable

x = "awesome"

 

def myfunc():

  x = "fantastic"

  print("Python is " + x)

 

myfunc()

 

print("Python is " + x)

The global Keyword

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.

Example

If you use the global keyword, the variable belongs to the global scope:

def myfunc():

  global x

  x = "fantastic"

 

myfunc()

 

print("Python is " + x)

Also, use the global keyword if you want to change a global variable inside a function.

Example

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

x = "awesome"

 

def myfunc():

  global x

  x = "fantastic"

 

myfunc()

 

print("Python is " + x)

4.4 Exercise

1. What is the correct syntax to add the value 'Hello World' to 3 variables in one statement?

A. x, y, z = 'Hello World'

B. x = y = z = 'Hello World'

C. x|y|z = 'Hello World'

D. 'x' = 'y' = 'z' = 'Hello World'

2. What is the correct syntax to assign values to multiple variables in one line?

A. x, y, z = "Orange", "Banana", "Cherry"

B. x = y = z = "Orange", "Banana", "Cherry"

C. x|y|z = "Orange", "Banana", "Cherry"

D. 'x' = 'y' = 'z' = "Orange", "Banana", "Cherry"

3. Consider the following code:

fruits = ['apple', 'banana', 'cherry']

a, b, c = fruits

print(a)

What will be the result of a?

A. apple

B. banana

C. cherry

D. True

4. Consider the following code:

print('Hello', 'World')

What will be the printed result?

A. Hello, World

B. Hello World

C. HelloWorld

D. 'Hello', 'World'

5. Consider the following code:

a = 'Hello'

b = 'World'

print(a + b)

What will be the printed result?

A. a + b

B. Hello World

C. HelloWorld

D. 'Hello''World'

6. Consider the following code:

a = 4

b = 5

print(a + b)

What will be the printed result?

A. 45

B. 9

C. 4 + 5

D. Error

7. Consider the following code:

x = 'awesome'

def myfunc():

  x = 'fantastic'

myfunc()

print('Python is ' + x)

What will be the printed result?

A. Python is awesome

B. Python is fantastic

C. Python is x

D. Python is + x

8. Insert the correct keyword to make the variable x belong to the global scope.

  ______x

  x = "fantastic"

A. global variable 

B. global

C. Global

D. “Global” 

9. Consider the following code:

x = 'awesome'

def myfunc():

  global x

  x = 'fantastic'

myfunc()

print('Python is ' + x)

What will be the printed result?

A. Python is awesome

B. Python is fantastic

C. Python is True

D. Python is False

10. Write a Python program to swap the values of two variables, using a temporary variable.

Input:

            Number x is: 7

            Number y is: 8

Expected output:

            After swapped:

Number x is: 8

            Number y is:

11. Write a Python program that allows user input length and width of the rectangle.

Calculate and output the perimeter and area of the rectangle.

Input:

Length: 5

Width: 6

Expected output:

Perimeter: 22

Area: 30 

-----

Bài sau: Python căn bản (5): Data Types

-----

[Nội dung tham khảo từ w3schools, pynative và Internet]

Bạn muốn học Python căn bản tại Đà Lạt, liên hệ