python学习记录(一)
1.打印操作
>>> print('hello') hello >>> print(1+2) 3
2.字符串操作
①
print(1+2+'3') Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> print(1+2+'3') TypeError: unsupported operand type(s) for +: 'int' and 'str' #整数与字符串不能相加
②
>>> print('hello'*7) hellohellohellohellohellohellohello #整数能与字符串相乘 >>> print('hello'*7.0) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> print('hello'*7.0) TypeError: can't multiply sequence by non-int of type 'float' #浮点数不能与字符串相乘
3.类型转化
①字符串转化为整数
>>> print(int("2") + int("3")) 5
②输入数据+类型转化+数据相加
>>> float(input("num1:"))+float(input("num2:")) num1:30 num2:2 32.0
③关于报错
int(input("num1:")) num1:2.0 Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> int(input("num1:")) ValueError: invalid literal for int() with base 10: '2.0' #输入为浮点数,与int不符
④例子
>>> float("210"*int(input("enter a number:"))) enter a number:2 210210.0 #210210转化为浮点型
4.变量
变量赋初值+变量的删除+输入变量值
>>> a=1
>>> print(a)
1
>>> print(a+3)
4
>>> print(a*2)
2
>>> a="hello"
>>> print(a*2)
hellohello
>>> del a
>>> print(a)
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
print(a)
NameError: name 'a' is not defined
>>> a = input("number:")
number:2
>>> print(a)
2
5.赋值运算符
>>> a=1 >>> a+=2 >>> print(a) 3 >>> str1="abc" >>> str1+="def" >>> print(str1) abcdef
与c语言相似,但无++等运算符
练习戳下

浙公网安备 33010602011771号