Hướng dẫn print input python - in đầu vào python

Python cung cấp sẵn hàm

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 để giúp chúng ta nhập dữ liệu cần thiết từ bàn phím cho chương trình. Ví dụ:
val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0
để giúp chúng ta nhập dữ liệu cần thiết từ bàn phím cho chương trình. Ví dụ:

val = input("Enter your value: ")
print(val)
num = input ("Enter number: ")
print(num)
name1 = input("Enter name: ")
print(name1)
  
# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
Kết quả
Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  

Khi hàm input() thực thi, luồng chương trình sẽ bị dừng lại cho đến khi người dùng nhập ký tự gì đó vào.input() thực thi, luồng chương trình sẽ bị dừng lại cho đến khi người dùng nhập ký tự gì đó vào.

Lưu ý: Bất cứ thứ gì chúng ta nhập vào, hàm

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 sẽ chuyển đổi nó thành một chuỗi. Nếu chúng ta nhập một số nguyên thì hàm
val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 vẫn chuyển nó thành một chuỗi. Do đó, chúng ta có thể chuyển đổi sang kiểu dữ liệu số với các hàm được hỗ trợ sẵn trong Python. Ví dụ:
: Bất cứ thứ gì chúng ta nhập vào, hàm
val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0
sẽ chuyển đổi nó thành một chuỗi. Nếu chúng ta nhập một số nguyên thì hàm
val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0
vẫn chuyển nó thành một chuỗi. Do đó, chúng ta có thể chuyển đổi sang kiểu dữ liệu số với các hàm được hỗ trợ sẵn trong Python. Ví dụ:

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
Kết quả
Enter your value:5
Enter number:9
Enter name:gochocit.com
5
9.0
gochocit.com
type of val is  
type of num is  
type of name1 is  

Khi hàm input() thực thi, luồng chương trình sẽ bị dừng lại cho đến khi người dùng nhập ký tự gì đó vào.

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 thành integer, floatstringnhư sau:

# input
print("input integer:")
num1 = int(input())
num2 = int(input())
  
# printing the sum in integer
print("sum of num1 and num2 = ", num1 + num2)

# input
print("input float:")
num1 = float(input())
num2 = float(input())
  
# printing the sum in float
print("sum of num1 and num2 = ", num1 + num2)

# input
print("input string:")
string = str(input())
  
# output
print(string)
Kết quả
input integer:
5
2
sum of num1 and num2 =  7
input float:
2.1
5.0
sum of num1 and num2 =  7.1
input string:
gochocit.com
gochocit.com

Khi hàm input() thực thi, luồng chương trình sẽ bị dừng lại cho đến khi người dùng nhập ký tự gì đó vào.

Lưu ý: Bất cứ thứ gì chúng ta nhập vào, hàm

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 sẽ chuyển đổi nó thành một chuỗi. Nếu chúng ta nhập một số nguyên thì hàm
val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 vẫn chuyển nó thành một chuỗi. Do đó, chúng ta có thể chuyển đổi sang kiểu dữ liệu số với các hàm được hỗ trợ sẵn trong Python. Ví dụ:

Chúng ta cũng có thể chuyển đổi

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
0 thành integer, floatvà stringnhư sau:Cú pháp:

input().split(separator, maxsplit)

Nhập nhiều giá trị từ user trong Pythonseparator là dấu phân cách, mặc định là khoảng trắng. Tham số maxsplit là số giá trị tối đa được nhập vào từ user.

Sử dụng hàm split()

# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)

# taking two inputs at a time with comma (,)
x, y = input("Enter two values: ").split(",")
print("Number of boys: ", x)
print("Number of girls: ", y)

# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
 
# taking two inputs at a time
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
 
# taking multiple inputs at a time
# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of numbers: ", x)
Kết quả
Enter two values: 5 9
Number of boys:  5 
Number of girls:  9
Enter two values: 5,9
Number of boys:  5
Number of girls:  9
Enter three values: 1 7 3
Total number of students:  1
Number of boys is :  7
Number of girls is :  3
Enter two values: 9 8
First number is 9 and second number is 8
Enter multiple values: 7 8 1 2 3
List of numbers:  [7, 8, 1, 2, 3]

Giúp nhận được nhiều giá trị nhập vào từ user. Mỗi giá trị được cách nhau bởi một dấu phân cách (separator). Mặc định, separator là khoảng trắng. Cú pháp:

Trong đó, separator là dấu phân cách, mặc định là khoảng trắng. Tham số maxsplit là số giá trị tối đa được nhập vào từ user.

# taking two input at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)

# taking three input at a time
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
 
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
 
# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)
Kết quả
Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
0

Ví dụ:

Sử dụng list comprehension

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
4 để in dữ liệu ra màn hình. Cú pháp:

Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
1

Đây là cách đơn giản đề tạo một list trong Python. Nhưng chúng ta cũng có thể sử dụng để nhập nhiều giá trị vào chương trình từ user.

  • 2. Xuất (output) trong Python: các giá trị sẽ được chuyển thành chuỗi để in ra màn hình.
  • Python hỗ trợ sẵn hàm
    val = input("Enter your value:")
    num = input ("Enter number:")
    name1 = input("Enter name:")
    
    # Convert data type
    val = int(val)
    print(val)
    num = float(num)
    print(num)
    print(name1)
    
    # Printing type of input value
    print ("type of val is ", type(val))
    print ("type of num is ", type(num))
    print ("type of name1 is ", type(name1))
    
    4 để in dữ liệu ra màn hình. Cú pháp:
    : tham số tùy chọn, có thể có hoặc không. Xác định dấu phân cách giữa các value muốn in ra trong trường hợp có nhiều value. sep mặc định là ’ ‘.
  • Trong đó:: tham số tùy chọn, có thể có hoặc không. Xác định ký tự được in ra cuối cùng. end mặc định là ‘\n’.
  • value(s): các giá trị sẽ được chuyển thành chuỗi để in ra màn hình.: tham số tùy chọn, có thể có hoặc không. Xác định đối tượng nhận value để in ra. Mặc định là
    val = input("Enter your value:")
    num = input ("Enter number:")
    name1 = input("Enter name:")
    
    # Convert data type
    val = int(val)
    print(val)
    num = float(num)
    print(num)
    print(name1)
    
    # Printing type of input value
    print ("type of val is ", type(val))
    print ("type of num is ", type(num))
    print ("type of name1 is ", type(name1))
    
    5
    (được in ra màn hình).
  • sep=’ ’: tham số tùy chọn, có thể có hoặc không. Xác định dấu phân cách giữa các value muốn in ra trong trường hợp có nhiều value. sep mặc định là ’ ‘.: tham số tùy chọn, có thể có hoặc không. Nếu là Truethì không lưu value vào bộ nhớ đệm. Nếu là Falsethì lưu value vào bộ nhớ đệm. Mặc định là False.

Sử dụng hàm split():

Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
2
Kết quả
Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
3

Giúp nhận được nhiều giá trị nhập vào từ user. Mỗi giá trị được cách nhau bởi một dấu phân cách (separator). Mặc định, separator là khoảng trắng. Cú pháp:

Trong đó, separator là dấu phân cách, mặc định là khoảng trắng. Tham số maxsplit là số giá trị tối đa được nhập vào từ user.

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))
6.

Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
4
Kết quả
Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
5

Ví dụ:

Sử dụng list comprehension

Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
6
Kết quả
Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
7

Đây là cách đơn giản đề tạo một list trong Python. Nhưng chúng ta cũng có thể sử dụng để nhập nhiều giá trị vào chương trình từ user.toán tử %.

Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
8
Kết quả
Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  
type of num is  
type of name1 is  
9

2. Xuất (output) trong Python

  • Python hỗ trợ sẵn hàm
    val = input("Enter your value:")
    num = input ("Enter number:")
    name1 = input("Enter name:")
    
    # Convert data type
    val = int(val)
    print(val)
    num = float(num)
    print(num)
    print(name1)
    
    # Printing type of input value
    print ("type of val is ", type(val))
    print ("type of num is ", type(num))
    print ("type of name1 is ", type(name1))
    
    4 để in dữ liệu ra màn hình. Cú pháp:
  • Trong đó:
  • value(s): các giá trị sẽ được chuyển thành chuỗi để in ra màn hình.
  • sep=’ ’: tham số tùy chọn, có thể có hoặc không. Xác định dấu phân cách giữa các value muốn in ra trong trường hợp có nhiều value. sep mặc định là ’ ‘.
  • end=’\n’: tham số tùy chọn, có thể có hoặc không. Xác định ký tự được in ra cuối cùng. end mặc định là ‘\n’.