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:  12345
RUN 2:
Enter any value: IncludeHelp
Entered value is:  IncludeHelp
RUN 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常用語法和示例

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541828.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541828.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541828.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

關于node.js和npm 和nvm_byKL

關于node.js和npm 和nvm Node 是一個服務器端 JavaScript 解釋器,Node 本身運行 V8 JavaScript。V8 JavaScript 引擎是 Google 用于其 Chrome 瀏覽器的底層 JavaScript 引擎。 NPM是隨同NodeJS一起安裝的包管理工具,能解決NodeJS代碼部署上的很多問題&am…

php 查看擴展 代碼,[擴展推薦] 使用 PHP Insights 在終端查看 PHP 項目代碼質量

PHP Insights 是一個由 Nuno Maduro 發布的、可在控制臺進行 PHP 即時質量檢查的拓展包。在項目的 readme 文件中,可以發現 PHP Insights 的主要功能包含:代碼質量 與 代碼風格 分析一個針對于代碼 結構 和 復雜度 的漂亮的預覽界面在 Laravel、Symfon…

航空機票預訂c#代碼_航空公司座位預訂問題的C ++程序

航空機票預訂c#代碼Problem statement: Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows: 問題陳述:編寫一個程序來分配飛機上的乘客座位。 假設小型飛機的座位編號如下: 1 A B C…

linux命令之which

which這個命令可以說并不常用,它的作用是查看可執行文件的位置,并返回第一個搜索結果。可執行文件也就是指的某個系統命令,但是這個命令的位置必須是在PATH路徑里存在的。截圖中 ,pwd的位置在/bin/pwd,當然,這個路徑是…

線性代數向量乘法_向量的標量乘法| 使用Python的線性代數

線性代數向量乘法Prerequisite: Linear Algebra | Defining a Vector 先決條件: 線性代數| 定義向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a mat…

sonar掃描普通JAVA執行,SonarQube掃描源代碼的方法

SonarQube掃描源代碼的方法雷建鋒一、分析源代碼綜述一旦成功安裝了SonarQube平臺,您就可以開始安裝一個分析器并開始創建項目了。在第一次分析時,該平臺會自動創建一個項目。如果您需要在第一個分析之前在項目上設置一些配置,那么您可以選擇…

html的學習思維導圖

轉載于:https://www.cnblogs.com/lingdublog/p/6438088.html

php語言冒泡法,PHP實現冒泡排序算法的案例

PHP實現冒泡排序算法的案例發布時間:2020-10-23 17:39:38來源:億速云閱讀:84作者:小新這篇文章主要介紹PHP實現冒泡排序算法的案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定…

線性代數分塊矩陣求逆矩陣_單位矩陣屬性(AI = A)| 使用Python的線性代數

線性代數分塊矩陣求逆矩陣Prerequisites: 先決條件: Defining Matrix 定義矩陣 Identity matrix 身份矩陣 numpy.matmul( ) matrix multiplication numpy.matmul()矩陣乘法 In linear algebra, the identity matrix, of size n is the n n square matrix with one…

MySQL5.7.17的簡單配置文件

#編譯安裝mysql5.7.17 [rootweb_1 data]# cat ../my.cnf [client]port3307socket/data/3307/mysql.sock[mysqld]user mysqlbasedir /usr/local/mysqldatadir /data/3307/dataport3307server-id 1socket/data/3307/mysql.sockcharacter-set-server utf8log-error /data/33…

cubic-bezier_帶CSS中的示例的cube-bezier()函數

cubic-bezierIntroduction: 介紹: How many times have we come across the word function? Well, it would not be wrong to say a lot. The fact that functions are used in web development while developing a website or web page is very important. There…

php時間調用最簡單的,PHP調用時間通過引用不可避免?

給定以下接口:interface ISoapInterface {public static function registerSoapTypes( &$wsdl );public static function registerSoapOperations( &$server );}以及以下代碼:$soapProvider array( "FilePool", "UserList" );foreach( $soapProvi…

上手Caffe(一)

author:oneBite 本文記錄編譯使用caffe for windows 使用環境 VS2013 ultimate,win7 sp1,caffe-windows源碼(從github上下載caffe的windows分支,下載解壓之后,不要改變原有的目錄結構,因為solution rebuild時會使用文件的相對路徑…

使用JavaScript的圖像識別游戲

Today we are going to develop a fully functional image recognition game using JavaScript. JavaScript is the best fit choice since it is a web-based game. The game is totally based on event handling and event objects. 今天,我們將使用JavaScript開發…

php 判斷 in,tinkphp常用判斷條件in、notin、between、AND、OR

越來越多的人使用thinkphp框架開發應用,容易上手開發周期短,接下來吾愛編程為大家分享一下tinkphp常用判斷條件in、notin、between、AND、OR,有需要的小伙伴可以參考一下:in:{in name"Think.get.level" valu…

關于設置不同linux主機之間ssh免密登錄簡易方法

2019獨角獸企業重金招聘Python工程師標準>>> 在linux日常中,經常會有ssh鏈接其他主機服務器的action,也學習過大家日常用配置ssh免密登錄的方法。 小編今天在這里給大家介紹一種比較簡單的配置linux主機ssh免密登錄的方法。 兩臺主機的IP地址&#xff1a…

c語言指針++_C ++此指針| 查找輸出程序| 套裝1

c語言指針Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){int A 10;this* ptr;ptr &A;*ptr 0;cout << *ptr << endl;return 0;}Output: 輸出&#xff1a; main.cpp: In function ‘int main()’:main.cpp:7:5: e…

java自定義線程池池,線程池使用及自定義線程池

一 案例引申編寫代碼同時只允許五個線程并發訪問(以下文的函數為例子)private static void method() {System.out.println("ThreadName" Thread.currentThread().getName() "進來了");Thread.sleep(2000);System.out.println("ThreadName" Th…

long類型20位示例_Java Long類reverseBytes()方法與示例

long類型20位示例長類reverseBytes()方法 (Long class reverseBytes() method) reverseBytes() method is available in java.lang package. reverseBytes()方法在java.lang包中可用。 reverseBytes() method is used to returns the value generated by reversing the order o…

impala和mysql語法,impala CREATE TABLE語句

CREATE TABLE語句用于在Impala中的所需數據庫中創建新表。 創建基本表涉及命名表并定義其列和每列的數據類型。語法以下是CREATE TABLE語句的語法。 這里&#xff0c;IF NOT EXISTS是一個可選的子句。 如果使用此子句&#xff0c;則只有在指定數據庫中沒有具有相同名稱的現有表…