美文网首页
Python 变量和简单的数据类型

Python 变量和简单的数据类型

作者: CaptainRoy | 来源:发表于2018-11-12 15:26 被阅读2次
  • 打印 和注释
print('Hello Python!') # Hello Python!
  • 变量
message = 'Hello Python World!'
print(message) # Hello Python World!

message = '你好! 编程世界!'
print(message) # 你好! 编程世界!
  • 字符串
string1 = "This is a string"
string2 = 'This is also a string'
string3 = 'I told my friend, "Pyhton is my favorite language"'

print(string3) # I told my friend, "Pyhton is my favorite language"
  • 字母大小写
name = "abc love"
print(name.title()) # Abc Love
print(name.upper()) #  ABC LOVE
print(name.lower()) # abc love
  • 字符串拼接
firstName = "Roy"
lastName = "Captain"
name = firstName + " " + lastName
print(name) # Roy Captain
  • 制表符
print("\t Roy \t Captain")
print("Language: \nC\nC++\nJava\nPython")
  • 数字
print(2 ** 3) # 8 2的3次方

age = 18
message = "Happy " + str(age) + "td Birthday!"
print(message) # Happy 18td Birthday!

⚠️ Python 将程序中的任何内容统称为对象(object)

  • 输入
input("请输入账号: \n")
password = input("请输入密码: \n")
if password != "123456" :
    print("密码不正确")
else:
    print("登录成功")
  • 格式化输出
age = 5
print("我今年"+str(age)+"岁")
print("%d" % age)
height = 1.8
print("%f",height)
name = "roy"
print("%s",name)
print("我的名字叫%s 年龄: %d"%(name,age))

相关文章

网友评论

      本文标题:Python 变量和简单的数据类型

      本文链接:https://www.haomeiwen.com/subject/doqcfqtx.html