日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python常用语法和示例_使用Python中的示例进行输入和输出操作

發布時間:2025/3/11 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python常用语法和示例_使用Python中的示例进行输入和输出操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python常用語法和示例

A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available for Input.

一個程序需要與用戶交互以完成所需的任務; 這是使用輸入輸出功能完成的。 輸入是指程序用戶輸入的數據。 在python中,我們為Input提供了input()和raw_input()函數。

1)input()函數 (1) input() function)

Syntax:

句法:

input (expression)

If prompt is present, it is displayed on monitor, after which the user can provide data from keyboard. Input takes whatever is typed from the keyboard and evaluates it. As the input provided is evaluated, it expects valid python expression. If the input provided is not correct then either syntax error or exception is raised by python.

如果出現提示,它將顯示在監視器上,之后用戶可以從鍵盤提供數據。 輸入接受從鍵盤輸入的任何內容并對其求值。 在評估提供的輸入時,它需要有效的python表達式。 如果提供的輸入不正確,則python會引發語法錯誤或異常。

Example 1:

范例1:

# python input operations# user input x = input("Enter any value: ")# printing value print("Entered value is: ", x)

Output

輸出量

RUN 1: Enter any value: 12345 Entered value is: 12345RUN 2: Enter any value: IncludeHelp Entered value is: IncludeHelpRUN 3: Enter any value: Python is a progamming language. Entered value is: Python is a progamming language.

Example 2:

范例2:

# python input operations# just provide a value and entered value prints print(input())# provide another value x = input() print("Your input is: ", x)# prompting message for input val1 = input("Enter a value: ") val2 = input("Enter another value: ") val3 = input("Enter another value: ")# printing values print("val1 =", val1) print("val2 =", val2) print("val3 =", val3)

Output

輸出量

Hello Hello I'm Shivang! Your input is: I'm Shivang! Enter a value: 100 Enter another value: 23.45 Enter another value: Helllooooooo val1 = 100 val2 = 23.45 val3 = Helllooooooo

2)raw_input()函數 (2) raw_input() function)

This input method fairly works in older versions (like 2.x).

此輸入法在較舊的版本(如2.x)中相當有效。

Syntax:

句法:

raw_input (expression)

If prompt is present, it is displayed on the monitor after which user can provide the data from keyboard. The function takes exactly what is typed from keyboard, convert it to string and then return it to the variable on LHS of '='.

如果出現提示,則它會顯示在監視器上,之后用戶可以通過鍵盤提供數據。 該函數將完全采用鍵盤輸入的內容,將其轉換為字符串,然后將其返回到'='的 LHS上的變量。

Example: In interactive mode

示例:在交互模式下

>>>x=raw_input ('Enter your name: ')Enter your name: ABC

x is a variable which will get the string (ABC), typed by user during the execution of program. Typing of data for the raw_input function is terminated by enter key.

x是一個變量,它將獲取字符串(ABC),由用戶在程序執行期間鍵入。 用enter鍵終止raw_input函數的數據輸入 。

We can use raw_input() to enter numeric data also. In that case we typecast, i.e., change the data type using function, the string data accepted from user to appropriate Numeric type.

我們也可以使用raw_input()輸入數字數據。 在那種情況下,我們進行類型轉換,即使用函數將數據類型(用戶接受的字符串數據更改為適當的數字類型)。

Example:

例:

>>>y=int(raw_input("Enter your roll no."))Enter your roll no. 5

It will convert the accepted string i.e., 5 to integer before assigning it to 'y'.

它將接受的字符串(即5)轉換為整數,然后將其分配給“ y”。

print()函數/聲明 (print() function/statement)

print evaluates the expression before printing it on the monitor. Print statement outputs an entire (complete) line and then goes to next line for subsequent output (s). To print more than one item on a single line, comma (,) may be used.

print先計算表達式,然后再將其打印在監視器上。 Print語句輸出整行(完整),然后轉到下一行以進行后續輸出。 要在一行上打印多個項目,可以使用逗號(,)。

Syntax:

句法:

print (expression/constant/variable)

Example 1:

范例1:

# print() example in Python# using single quotes print('Hello!') print('How are you?')# using double quotes print("Hello!") print("How are you?")# using triple single quotes # those can be used to print multiple line string print('''Hello!''') print('''How are you?''')# printing multiline string print('''Hello... how are you? Hey! I am good, what about you? I am good also, thanks.''')

Output

輸出量

Hello! How are you? Hello! How are you? Hello! How are you? Hello... how are you? Hey! I am good, what about you? I am good also, thanks.

Example 2:

范例2:

# print() example in Python# printing values print("Printing direct values...") print(10) # printing an integer print(10.2345) # printing a float print([10, 20, 30, 40, 50]) # printing a list print({10, 20, 30, 40, 50}) # printing a set# printing variables a = 10 b = 10.2345 c = [10, 20, 30, 40, 50] d = {10, 20, 30, 40, 50}print("Printing variables...") print(a) print(b) print(c) print(d)# printing message with variables print("Printing message variables...") print("a = ", a) print("b = ", b) print("c = ", c) print("d = ", d)

Output

輸出量

Printing direct values... 10 10.2345 [10, 20, 30, 40, 50] {40, 10, 50, 20, 30} Printing variables... 10 10.2345 [10, 20, 30, 40, 50] {40, 10, 50, 20, 30} Printing message variables... a = 10 b = 10.2345 c = [10, 20, 30, 40, 50] d = {40, 10, 50, 20, 30}

Recommended posts

推薦的帖子

  • Read input as an integer in Python

    在Python中將輸入讀取為整數

  • Read input as a float in Python

    在Python中以浮點形式讀取輸入

  • Parse a string to float in Python (float() function)

    解析要在Python中浮動的字符串(float()函數)

  • How do you read from stdin in Python?

    您如何從Python的stdin中讀取信息?

  • Asking the user for integer input in Python | Limit the user to input only integer value

    要求用戶在Python中輸入整數| 限制用戶僅輸入整數值

  • Asking the user for input until a valid response in Python

    要求用戶輸入直到Python中的有效響應

  • Input a number in hexadecimal format in Python

    在Python中以十六進制格式輸入數字

  • Input a number in octal format in Python

    在Python中以八進制格式輸入數字

  • Input a number in binary format in Python

    在Python中以二進制格式輸入數字

  • How to get the hexadecimal value of a float number in python?

    如何在python中獲取浮點數的十六進制值?

  • Convert an integer value to the string using str() function in Python

    使用Python中的str()函數將整數值轉換為字符串

  • Convert a float value to the string using str() function in Python

    使用Python中的str()函數將浮點值轉換為字符串

  • Taking multiple inputs from the user using split() method in Python

    使用Python中的split()方法從用戶獲取多個輸入

  • Fast input / output for competitive programming in Python

    快速輸入/輸出,可在Python中進行有競爭力的編程

  • Precision handling in Python

    Python中的精確處理

  • Python print() function with end parameter

    帶有結束參數的Python print()函數

翻譯自: https://www.includehelp.com/python/input-and-output-operations-with-examples.aspx

python常用語法和示例

總結

以上是生活随笔為你收集整理的python常用语法和示例_使用Python中的示例进行输入和输出操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。