Blog3@python I/O 操作

接下来我们将简单了解一些语法及其结构,同时亦会通过例题来学习,建议在学习的过程中多练习哈~

一、 IO操作
1. 关于输入输出
Input:
raw_input:传入值为字符串类型;
input: 传入值为数值类型;
Output:
print “hello world”
print hello #此时会显示报错,因为python默认hello为一个变量

    print """
                            xxxxx

            1. 
            2.
    """

注:此时三个引号表示格式化输出

2.常用占位符
占位符: %s, %d, %f, %.5d, %.2f, %e
name = “westos”
age = 19
money = 1000.2345
print “Name:%s Age:%d Money:%.2f” %(name, age, money)

注:%.2f表示小数点后保留两位

例题:
1.input接收数值类型数据
判断闰年?
这个年份能被4整除但不能被100整除, 或者能被400整除,那么就是闰年.

#!/usr/bin/env python
#coding:utf-8
year = input("输入判断年份:")
print type(year)
print (year%4==0 and year%100!=0) or  year%400==0

2.raw_input接收字符串类型的数据:

#!/usr/bin/env python
#coding:utf-8
username = raw_input("用户名:")
password = raw_input("密码:")                                                  
print "username:%s    password:%s" %(username, password)

print "用户名是:", username
print "密码是:", password

3.输出print

#!/usr/bin/env python
#coding:utf-8
print "1. %s       2. %s"   %("Clanguage","python")
print """
1. %s 
2. %s
"""%("xixi","haha")

4 .
要求:输入某学生的三门课程成绩,计算出该学
生的平均成绩。
提示:(course1+course2+course3)/3

#!/usr/bin/env python
#coding:utf-8

score1 = input("科目1:")
score2 = input("科目2:")
score3 = input("科目3:")
print (score1 + score2 + score3)/3

5.
写一程序,录入信息包括hostname、IP、
used_year、CPU、Memory、manager_name,
如果使用年限超过10年,直接显示警告信息“该服务
器使用年限太久!”,如果使用年限不超过10年,显
示该服务器信息如下面的格式如下:
主机信息
主机名: hostname
IP:IP
使用年限:used_year
CPU:CPU
Memory:Memory

#!/usr/bin/env python
#coding:utf-8

print "****************服务器信息录入*******************"
hostname = raw_input("把主机名告诉我:")
IP = raw_input("你ip是啥:")
used_year = input("已使多少年:")                                                
if used_year > 10:
        print ("该服务器使用年限太久!")
else:
        CPU = raw_input("cpu类型:")
        Memory = input ("内存多大:")
        print """
                服务器信息显示
hostname:%s
IP:%s
used_year:%s
CPU:%s
Memory:%s
""" %(hostname,IP,used_year,CPU,Memory)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Python中,selectors模块提供了一种高级的I/O多路复用机制,用于管理多个文件描述符的状态。它基于底层的select系统调用,可以同时监视多个文件描述符的可读、可写和异常事件。\[1\]在给定的一组文件描述符上调用select函数,可以等待其中任何一个文件描述符就绪,然后执行相应的操作。select函数的原型如下所示:int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);\[2\]与select不同,selectors模块提供了更加高级和灵活的接口,可以使用回调函数来处理就绪的文件描述符。这样可以避免阻塞,实现异步I/O的效果。selectors模块的使用方式类似于事件驱动的编程模型,可以通过注册回调函数来处理不同的事件。\[1\]相比于select,poll和epoll,selectors模块提供了更加简洁和易用的接口,可以更方便地实现高效的I/O多路复用。\[3\] #### 引用[.reference_title] - *1* *2* *3* [Python之路 35:事件驱动模型、IO模型、selectors模块(IO复用、select、poll、epoll)](https://blog.csdn.net/qq_55535816/article/details/122785856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值