Python căn bản (3): Variables

Bài trước: Python căn bản (2): Syntax

-----

[Từ điển]

3. Python Variables

3.1 Variables

Variables are containers for storing data values.

Creating variables

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Example

x = 5

y = "John"

print(x)

print(y)

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example

x = 4       # x is of type int

x = "Sally" # x is now of type str

print(x)

Casting

If you want to specify the data type of a variable, this can be done with casting.

Example

x = str(3)    # x will be '3'

y = int(3)    # y will be 3

z = float(3)  # z will be 3.0

Get the type

You can get the data type of a variable with the type() function.

Example

x = 5

y = "John"

print(type(x))

print(type(y))

Single or Double Quotes?

String variables can be declared either by using single or double quotes:

Example

x = "John"

# is the same as

x = 'John'

print(x)

Case-Sensitive

Variable names are case-sensitive.

Example

This will create two variables:

a = 4

A = "Sally"

#A will not overwrite a

print(a)

print(A)

3.2 Variable Name

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

- A variable name must start with a letter or the underscore character

- 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 (age, Age and AGE are three different variables)

- A variable name cannot be any of the Python keywords.

Example

Legal variable names:

myvar = "John"

my_var = "John"

_my_var = "John"

myVar = "John"

MYVAR = "John"

myvar2 = "John"

Example

Illegal variable names:

2myvar = "John"

my-var = "John"

my var = "John"

Multi Words Variable Names

Variable names with more than one word can be difficult to read.

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

Camel Case

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

Example

myVariableName = "John"

Pascal Case

Each word starts with a capital letter:

Example

MyVariableName = "John"

Snake Case

Each word is separated by an underscore character:

Example

my_variable_name = "John"

3.3 Input data from the keyboard

The input() function is the simplest way to get keyboard data from the user in Python. 

When called, it asks the user for input with a prompt that you specify, and it waits for the user to type a response and press the Enter key.

Example

name = input("Your name: ")

print(name)

3.4 Exercise

1. What is a correct way to declare a Python variable?

A. var x = 5 

B. #x = 5

C. x = 5

D. $x = 5

2. True or False:

You can declare string variables with single or double quotes.

x = "John"

# is the same as

x = 'John'

A. True

B. False

3. True or False:

Variable names are not case-sensitive.

a = 5

# is the same as

A = 5

A. True

B. False

4. Which is NOT a legal variable name?

A. my-var = 20

B. my_var = 20

C. Myvar = 20

D. _myvar = 20

5. Create a variable named carname and assign the value Volvo to it. Which is correct?

A. carname = Volvo

B. “carname” = “Volvo”

C. carname = “Volvo”

D. “carname” = Volvo

6. Create a variable named x and assign the value 50 to it. Which is correct?

A. x  = “50”

B. x = 50

C. “x” = “50”

D. “x” = 50

7. Creating two variables to store your age and your full name. Print values of two variables out screen. Hints: variables named myAge, myFullname.

Expected output:

11 Nguyen Van Teo

8. Using input() function to get data from the keyboard. Example:

myAge = input(“So tuoi cua ban la: “)

Write the program to get the: full name, age, phone number, address from the keyboard, then print out the screen.

Expected output:

Ho ten cua ban la: Nguyen Van Teo

Tuoi cua ban la:10

So dien thoai cua ban la:0988765432

Dia giao hang la:12 Bui Thi Xuan - Dalat

Thong tin cua ban:

 

 Nguyen Van Teo 10 0988765432 12 Bui Thi Xuan - Dalat

----- Cập nhật 16/9/2024 -----

Bài sau: Python căn bản (4): Variables (cont)

-----

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