4.从输入输出到基本运算符的学习

cnblogs 2024-09-02 10:09:00 阅读 51

从输入输出到运算符的学习,有一种拨云见月的感觉来了

主要是输入输出的内容

<code>1.交互式输入

2.格式化输出

1 占位符输出

2 format输出

3 f-string输出

3.基本运算符

1.算术运算符

2.比较运算符

3.赋值运算符

4.逻辑运算符

5.身份运算符

4.赋值方法

1.链式赋值

2.交叉赋值

5.解压缩

6.作业

1 交互式输入

这里主要讲的是与input()函数的交互,其实交互的意思就是可以让用户个人来提供输入信息

name = input("name<<<")

age = input('age<<<')

height = input('height<<<')

print("my name is:"+name+", my age is:"+age+", my height is"+height+'.')

name<<<xiaocao

age<<<22

height<<<170

my name is:xiaocao, my age is:22, my height is170.

print("my name is:"+name+", my age is:"+age+", my height is"+height+'.')

print('*'*100)

print(type(name))

print(type(age))

print(type(height))

my name is:xiaocao, my age is:22, my height is170.

****************************************************************************************************

<class 'str'>

<class 'str'>

<class 'str'>

这里主要注意input的输出类型为str,可以直接使用在input前面增加int,float进行数据类型转换

print(type(int(age)))

<class 'int'>

2 格式化输出

这里三种占位符,format和f-string

还是用上面的例子来打比方

2.1 占位符

name = input("name<<<")

age = input('age<<<')

height = input('height<<<')

print("my name is %s, my age is %s, my height is %s."%(name,age,height))

name<<<xiaocao

age<<<18

height<<<170

my name is xiaocao, my age is 18, my height is 170.

name = input("name<<<")

age = int(input('age<<<'))

height = int(input('height<<<'))

print("my name is %s, my age is %d, my height is %d."%(name,age,height))

name<<<xiaocao

age<<<18

height<<<170

my name is xiaocao, my age is 18, my height is 170.

%d占位符表示的是数字,如果是字符串类型则会报错

2.2 format形式

这个就是将占位符更换成大括号了

与占位符的区别:可以调整顺序

print("my name is {}, my age is {}, my height is {}.".format(name,age,height))

print("my age is {1}, my name is {0}, my height is {2}.".format(name,age,height))

print("my name is {}, my age is {}, my height is {:.2f}.".format(name,age,height))

my name is xiaocao, my age is 18, my height is 170.

my age is 18, my name is xiaocao, my height is 170.

my name is xiaocao, my age is 18, my height is 170.00.

以前:format的形式好难呀,看到就头疼

现在:?怎么可以这么清晰,而且还可以用markdown来写文档了

2.3 f-string的形式

哇,这个简直就是绝,直接在大括号内部添加内容

name = input("name<<<")

age = input('age<<<')

height = input('height<<<')

print(f"my name is {name}, my age is {age}, my height is {height}.")

name<<<ciaocao

age<<<18

height<<<170

my name is ciaocao, my age is 18, my height is 170.

print(f"my name is {name}, my age is {int(age)+1}, my height is {height * 10}.")

my name is ciaocao, my age is 19, my height is 170170170170170170170170170170.

print(f"my name is {name}, my age is {age+1 :.2f}, my height is {height * 10}.")

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

Cell In[15], line 1

----> 1 print(f"my name is {name}, my age is {age+1 :.2f}, my height is {height * 10}.")

TypeError: can only concatenate str (not "int") to str

print(f"my name is {name}, my age is {int(age)+1 :.2f}, my height is {height * 10}.")

my name is ciaocao, my age is 19.00, my height is 170170170170170170170170170170.

虽然这个例子有些不合理吧,但是表达的意思倒是听清楚的

哈哈哈哈哈哈哈哈哈哈哈哈

保留报错的原因,是因为这个很经典,忘记将其转换为数字类型报错啦

3 基本运算符

3.1 算术运算符

顾名思义,用于计算数值间的值的运算符

1+2

3

1-2

-1

1/2

0.5

1//2

0

1%2

1

3.2 比较运算符

用于比较数值大小用的

1>2

False

2>1

True

7>=2

True

7<=2

False

8==8

True

1 != 2

True

3.3 赋值运算符

就是将数值赋值给一个变量

y =5

print(y)

5

3.4 逻辑运算符

三种形式 and or not

1>1 and 1>2

False

1>1 or 1<2

True

#0表示的就是错的意思

not 0

True

3.5 身份运算符

一般就是比较两个id是否相同,用 is 来表示

x = 300

y = 300

print(x == y)

print(id(x)==id(y))

print (x is y)

True

False

False

4 赋值方式

这一块主要说明这些方法较一些C语言而言要简单

链式赋值

交叉赋值

4.1 链式赋值

这个好理解,跟一条链子一样,左边有几个变量右边就有几个数值

#这样会很麻烦

x = 10

y = 10

z = 10

print(x,y,z)

a ,b =10,20

print(a,b)

10 10 10

10 20

4.2 交叉赋值

这个就是说白了,更换变量

a = 10

b = 20

print(a,b)

10 20

a = 10

b = 20

c = a

a = b

b = c

print(a,b)

20 10

a,b =b,a

print(a,b)

10 20

5 解压缩

解压缩的目的就是在列表里面中间不要的信息过多,需要进行忽略,只留下对我们而言重要的信息

方法1:日常使用过程,如果想给列表里的每个值进行出来,则可以使用

hobby = ['piao','666']

x ,y =hobby

print(x,y)

piao 666

如果取出来有我们不想要的值,则可以用下划线来代替

hobby_list = ['piao','666',2333,999,'handsome']

_,hobby1,_,_,hobby2 = hobby_list

print(hobby1,hobby2)

666 handsome

如果在使用过程中取中间两个数,其他数字进行省略可以用星号加下划线

hobby_list = ['piao','666',2333,999,'handsome','read','run','music','swimming','football','basketball','baterminten']

_,hobby1,_,hobby2,*_ = hobby_list

print(hobby1,hobby2)

666 999

6 作业

6.1 作业1

用三种格式化方式进行输出

name = 'xiaocao'

height = 180

weight = 100

print("My name is %s, my height is %d, my weight is %d"%(name,height,weight))

print("My name is {}, my height is {}, my weight is {}".format(name,height,weight))

print(f"My name is {name}, my height is {int(height)}, my weight is {weight}")

My name is xiaocao, my height is 180, my weight is 100

My name is xiaocao, my height is 180, my weight is 100

My name is xiaocao, my height is 180, my weight is 100

6.2 一行代码实现交互

x = 10

y = 10

z = 10

x,y,z =10,10,10

print(x,y,z)

10 10 10

6.3 交换数值

x = 10

y = 10

x = 10

y = 20

x,y =y,x

print(x,y)

20 10

6.4 取出想要的爱好

hobby_list = ['read','run','music','code']

hobby_list = ['read','run','music','code']

print(hobby_list[1:3])

_,hobby1,hobby2,_ = hobby_list

print(hobby1,hobby2)

['run', 'music']

run music

总结

1.首先是markdown语法的学习,成功开始开启我的博客生涯

2.关于计算机的系统,有了一个更清晰的认识

3.使用jupyter开始写代码

4.常量和变量的定义,内存的存储方式,数据类型

1.内存存储会计数,有的id会相同有的不会

2.数字类型(整数和浮点数),字符串类型,列表类型,字典类型,布尔类型

5.关于编程的输入输出以及基本运算符

1.交互式输入,即可以让用户看到,用input函数

2.输出三种类型,占位符,format和f-string,掌握的是f-string

3.基本运算符五种,算数,比较,赋值,逻辑和地址

6.数值赋值的两种方式:链式和交叉

7.解压缩,我所掌握的一个点就是下划线在这里是表示省略内部的一些数据的意思



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。