Python變量定義


python的輸出語句

1.支持雙引號、單引號、三引號

print("hello world")
print('hello python')
print("""Linux
windows
MacOS""")
print('''Nginx
Httpd
Tomcat''')
print("This is 'python'demo!!!") # 外面雙引號,里面單引號
print("python自動化運維")

2.輸出變量的值

username="Joke"
print(username)
print("hello",username)

3.格式化輸出

(1.)%s 字符串格式化輸出

username = "Joke"
age = 30
print("hello %s" % username)
print("My name is %s,My age is %s" %(username,age))

(2.)%d 整數格式化輸出

print("Thist number is %d" % 20)
print("This number is %d" % 3.14)  # 輸出為3

(3.) %f 浮點數格式化輸出

print("The number is %f" % 10)
print("The number is %f" % 3.14)
print("The number is %.3f" % 3.1415926)  # %.3f 表示保留幾位數據

(4.)輸出%本身

number = 50
print("This is %d%%" % number) # %%會輸出%

4.變量定義

格式: 變量名稱 = 變量值

變量名規范

  1. 只能包含字母、數字、下划線
  2. 只能以字母或者下划線開頭
  3. 見名知義
  4. 不能用python關鍵字,os,shutil,for,int,str

調用變量:直接通過變量名進行變量的值

username = "Jone"

print(username)

變量特性:

(1.)弱類型變量定義

(2.)地址引用類型

number1=100
print(id(number1))  # 使用id()獲取變量內存地址
------------------------------------------
# 變量替換
a = 10 
b = 20
a,b = b,a
print(a,b)  # 20,10

5.交互式變量定義

變量名 = input("提示信息")

注意:intput()默認返回的數據類型為字符串

name = input("請輸入用戶名:")
print("hello %s" % name)
------------------------------
data = input("數據>>>")
print(data)
print(type(data))  # 輸出str,使用type()可以查看變量類型


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM