1、变量
在程序中可随时修改变量的值,python始终记录变量的最新值
变量名:必须以字母或者下划线开头,只能包含字母数字和下划线,建议使用下划线+小写字母的方式命名,不能为函数名和python关键字
message = "one" print(message) message = "two" print(message)
输出:
one
two
2、字符串
name = "hello world, I AM HAPPY hAhAhAHAha"
print(name.title()) #每个单词首字母大写
print(name.upper()) #全部转成大写
print(name.lower()) #全部转成小写
输出:
Hello World, I Am Happy Hahahahaha
HELLO WORLD, I AM HAPPY HAHAHAHAHA
hello world, i am happy hahahahaha
first_name = "jim" second_name = "green" full_name = first_name + " " + second_name message = "Hello, " + full_name.title() + "!" #字符串的拼接 print(message)
输出:
Hello, Jim Green!
print("Languages: Python C Java")
输出:
Languages:
Python
C
Java
favorite_language = " Python " print("before: ", favorite_language) print(favorite_language.lstrip()) #删除左边空格 print(favorite_language.rstrip()) #删除右边空格 print(favorite_language.strip()) #删除两边空格 print("after: ", favorite_language);
输出:
before: Python
Python
Python
Python
after: Python
message = "hello, I'm crazybird123..." print(message)
输出:hello, I'm crazybird123...
message = 'hello, I am "crazybird123"!' #单引号里面用双引号输出"crazybird123"
print(message)
输出:
hello, I am "crazybird123"!
3、数字
print( 2 ** 5 ) #2的5次方 print( 2 + 3 * 4 ) print( (2 + 3) * 4 ) print(0.1 + 0.2) #浮点数小数位不确定
输出:
32
14
20
0.30000000000000004
age = 25 message = "Happy " + str(age) + "rd Birthday!" #str()将非字符串类型转换成字符串 print(message)
输出:Happy 25rd Birthday!
height = 180.009 message = "He is " + str(height) + " meters!" print(message)
输出:He is 180.009 meters!
整数除法:python2和python有区别
python3中:print(3/2) 输出是1.5 而在pyuthon2中输出确实1。而在python3中使用//表示floor除法,即3//2结果为1