可爱的python 大合集

简介: 计算今年是闰年嘛?判断闰年条件, 满足年份模400为0, 或者模4为0但模100不为0. 点击(此处)折叠或打开 #!/usr/bin/env python #-*- coding:utf8...
计算今年是闰年嘛?判断闰年条件, 满足年份模400为0, 或者模4为0但模100不为0.

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf8 -*-
  3. import time

  4. #thisyear=2017
  5. thisyear=time.localtime()[0]
  6. if thisyear % 400 == 0 or thisyear % 4 == 0 and thisyear % 100 > 0:
  7.   print('%s 是闰年' % thisyear)
  8. else:
  9.   print('%s 不是闰年' % thisyear)
[t@localhost Documents]$ python 5.1.py
2016 是闰年
[t@localhost Documents]$ python 5.1.py
2017 不是闰年
利用python作为科学计算器。熟悉Python中的常用运算符,并分别求出表达式12*34+78-132/6、(12*(34+78)-132)/6、(86/40)**5的值。并利用math模块进行数学计算,分别求出145/23的余数,0.5的sin和cos值(注意sin和cos中参数是弧度制表示)提醒:可通过import math; help("math")查看math帮助.

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf8 -*-
  3. a=12*34+78-132/6
  4. b=(12*(34+78)-132)/6
  5. c=(86/40) ** 5
  6. print("12*34+78-132/6= %d " % a)
  7. print("(12*(34+78)-132)/6= %d " % b)
  8. print("(86/40) ** 5= %f " % c)
  9. import math

  10. d=math.fmod(145,23)
  11. e=math.sin(0.5)
  12. f=math.cos(0.5)
  13. print("145/23的余数是%f"%d)
  14. print("sin(0.5)=%f"%e)
  15. print("cos(0.5)=%f"%f)
[t@localhost Documents]$ python 5.1.2.py  
12*34+78-132/6= 464  
(12*(34+78)-132)/6= 202  
(86/40) ** 5= 32.000000  
145/23的余数是7.000000
sin(0.5)=0.479426
cos(0.5)=0.877583

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*-coding:utf8-*-
  3. import math
  4. def is_prime(n):
  5.     result=[]
  6.     for inum in range(2,n):
  7.         for num in range(2,int(math.sqrt(n))+1):
  8.             if inum % num == 0 and inum != num:
  9.                 break
  10.             elif inum % num !=0 and num == int(math.sqrt(n)-1):
  11.                 result.append(inum)
  12.     return result
  13. print(is_prime(100))
[t@localhost tmp]$ python 5.1.2.py  
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

CDays-4


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*-coding:utf8-*-
  3. import os
  4. f=open('a.txt','a')
  5. for root,dirs,files in os.walk('/boot'):
  6.     print root,dirs,files
  7.     f.write("%s %s %s\n" %(root,dirs,files))
[t@localhost tmp]$ python cday4.1.py    
/boot ['lost+found', 'extlinux', 'efi', 'grub2'] ['System.map-4.5.0-0.rc7.git0.2.fc24.x86_64', 'config-4.5.0-0.rc7.git0.2.fc24.x86_64', 'initramfs-4.5.0-0.rc7.git0.2.fc
24.x86_64.img', 'elf-memtest86+-5.01', '.vmlinuz-4.5.0-0.rc7.git0.2.fc24.x86_64.hmac', 'memtest86+-5.01', 'vmlinuz-4.5.0-0.rc7.git0.2.fc24.x86_64', 'initrd-plymouth.img
']
/boot/extlinux [] ['chain.c32', 'pmload.c32', 'libcom32.c32', 'config.c32', 'host.c32', 'pxechn.c32', 'kontron_wdt.c32', 'menu.c32', 'mboot.c32', 'elf.c32', 'dhcp.c32',
'ethersel.c32', 'ifcpu64.c32', 'vesamenu.c32', 'vesainfo.c32', 'cmd.c32', 'cat.c32', 'ifcpu.c32', 'libutil.c32', 'linux.c32', 'whichsys.c32', 'libgpl.c32', 'cpuidtest.
c32', 'sysdump.c32', 'liblua.c32', 'sdi.c32', 'lua.c32', 'debug.c32', 'dmitest.c32', 'vpdtest.c32', 'syslinux.c32', 'vesa.c32', 'pci.c32', 'hdt.c32', 'gpxecmd.c32', 'dm
i.c32', 'memdisk', 'cpu.c32', 'lfs.c32', 'cmenu.c32', 'meminfo.c32', 'poweroff.c32', 'kbdmap.c32', 'pcitest.c32', 'rosh.c32', 'ifmemdsk.c32', 'gfxboot.c32', 'prdhcp.c32
', 'ifplop.c32', 'libmenu.c32', 'cpuid.c32', 'cptime.c32', 'reboot.c32', 'hexdump.c32', 'sanboot.c32', 'ls.c32', 'pwd.c32', 'zzjson.c32', 'ldlinux.c32', 'disk.c32']
/boot/efi ['System', 'EFI'] ['mach_kernel']
/boot/efi/System ['Library'] []
/boot/efi/System/Library ['CoreServices'] []
/boot/efi/System/Library/CoreServices [] ['SystemVersion.plist']
/boot/efi/EFI ['fedora', 'BOOT'] []
/boot/efi/EFI/fedora ['fonts'] ['shim.efi', 'gcdx64.efi', 'grubenv', 'MokManager.efi', 'grubx64.efi', 'shim-fedora.efi', 'BOOT.CSV']
/boot/efi/EFI/fedora/fonts [] ['unicode.pf2']
/boot/efi/EFI/BOOT [] ['BOOTX64.EFI', 'fallback.efi']
/boot/grub2 ['i386-pc', 'locale', 'themes', 'fonts'] ['device.map', 'grubenv', 'grub.cfg']
/boot/grub2/i386-pc [] ['cmdline_cat_test.mod', 'parttool.mod', 'ntldr.mod', 'bfs.mod', 'msdospart.mod', 'lvm.mod', 'gcry_idea.mod', 'xnu.mod', 'net.mod', 'videoinfo.mo
d', 'pxechain.mod', 'crypto.mod', 'terminal.lst', 'cpuid.mod', 'sfs.mod', 'tar.mod', 'multiboot2.mod', 'setjmp_test.mod', 'http.mod', 'jfs.mod', 'spkmodem.mod', 'usbtes
t.mod', 'tga.mod', 'syslinuxcfg.mod', 'search_fs_uuid.mod', 'gcry_whirlpool.mod', 'keylayouts.mod', 'fs.lst', 'gcry_tiger.mod', 'password.mod', 'exfat.mod', 'video_colo
rs.mod', 'plan9.mod', 'datehook.mod', 'part_bsd.mod', 'usb.mod', 'aout.mod', 'tftp.mod', 'datetime.mod', 'halt.mod', 'adler32.mod', 'gcry_sha512.mod', 'part_msdos.mod',
'play.mod', 'crc64.mod', 'crypto.lst', 'sleep.mod', 'hello.mod', 'gptsync.mod', 'cat.mod', 'usbserial_pl2303.mod', 'chain.mod', 'relocator.mod', 'uhci.mod', 'testload.
mod', 'video_cirrus.mod', 'linux.mod', 'gcry_crc.mod', 'reboot.mod', 'minix.mod', 'minicmd.mod', 'exfctest.mod', 'functional_test.mod', 'lsacpi.mod', 'gcry_blowfish.mod
', 'part_apple.mod', 'minix3_be.mod', 'macho.mod', 'part_dvh.mod', 'videotest.mod', 'multiboot.mod', 'iso9660.mod', 'cmp.mod', 'pbkdf2.mod', 'mdraid09_be.mod', 'ata.mod
', 'search_fs_file.mod', 'ufs2.mod', 'blscfg.mod', 'bitmap.mod', 'ohci.mod', 'div.mod', 'ufs1.mod', 'dm_nv.mod', 'minix_be.mod', 'elf.mod', 'shift_test.mod', 'serial.mo
d', 'gcry_sha1.mod', 'password_pbkdf2.mod', 'pci.mod', 'part_sunpc.mod', 'help.mod', 'gcry_rmd160.mod', 'ahci.mod', 'disk.mod', 'part_sun.mod', 'drivemap.mod', 'read.mo
d', 'sendkey.mod', 'vga.mod', 'legacy_password_test.mod', 'probe.mod', 'usbms.mod', 'video.mod', 'gfxterm_background.mod', 'ext2.mod', 'odc.mod', 'mul_test.mod', 'ufs1_
be.mod', 'minix2_be.mod', 'time.mod', 'memrw.mod', 'mdraid1x.mod', 'mmap.mod', 'ldm.mod', 'btrfs.mod', 'vbe.mod', 'part_dfly.mod', 'bitmap_scale.mod', 'cpio_be.mod', 'b
ufio.mod', 'hdparm.mod', 'gcry_twofish.mod', 'minix3.mod', 'hfspluscomp.mod', 'usbserial_common.mod', 'hfs.mod', 'pcidump.mod', 'gcry_camellia.mod', 'cmp_test.mod', 'fs
help.mod', 'moddep.lst', 'lzopio.mod', 'usb_keyboard.mod', 'command.lst', 'affs.mod', 'priority_queue.mod', 'loopback.mod', 'partmap.lst', 'file.mod', 'gcry_des.mod', '
cbls.mod', 'gfxmenu.mod', 'ntfscomp.mod', 'fat.mod', 'xnu_uuid.mod', 'setjmp.mod', 'parttool.lst', 'png.mod', 'ls.mod', 'lspci.mod', 'zfscrypt.mod', 'gfxterm.mod', 'off
setio.mod', 'reiserfs.mod', 'part_plan.mod', 'gettext.mod', 'afs.mod', 'verify.mod', 'mpi.mod', 'random.mod', 'xfs.mod', 'gdb.mod', 'gcry_arcfour.mod', 'pbkdf2_test.mod
', 'hfsplus.mod', 'diskfilter.mod', 'gcry_md4.mod', 'archelp.mod', 'gcry_seed.mod', 'regexp.mod', 'zfsinfo.mod', 'part_gpt.mod', 'gcry_md5.mod', 'video.lst', 'normal.mo
d', 'efiemu32.o', 'setpci.mod', 'signature_test.mod', 'pxe.mod', 'gcry_dsa.mod', 'cbtable.mod', 'search_label.mod', 'acpi.mod', 'boot.mod', 'gcry_rfc2268.mod', 'modinfo
.sh', 'sleep_test.mod', 'cmostest.mod', 'gfxterm_menu.mod', 'trig.mod', 'all_video.mod', 'video_bochs.mod', 'cryptodisk.mod', 'ctz_test.mod', 'test_blockarg.mod', 'vide
o_fb.mod', 'bsd.mod', 'usbserial_ftdi.mod', 'cbfs.mod', 'loadenv.mod', 'font.mod', 'zfs.mod', 'mdraid09.mod', 'configfile.mod', 'gcry_rsa.mod', 'lsmmap.mod', 'iorw.mod'
, 'true.mod', 'raid6rec.mod', 'gcry_rijndael.mod', 'core.img', 'echo.mod', 'luks.mod', 'boot.img', 'cpio.mod', 'scsi.mod', 'part_amiga.mod', 'pata.mod', 'videotest_chec
ksum.mod', 'procfs.mod', 'usbserial_usbdebug.mod', 'hashsum.mod', 'xnu_uuid_test.mod', 'xzio.mod', 'cmosdump.mod', 'at_keyboard.mod', 'mda_text.mod', 'efiemu.mod', 'gcr
y_sha256.mod', 'div_test.mod', 'cbmemc.mod', 'cs5536.mod', 'udf.mod', 'raid5rec.mod', 'linux16.mod', 'date.mod', 'hexdump.mod', 'nativedisk.mod', 'gzio.mod', 'ehci.mod'
, 'gcry_serpent.mod', 'search.mod', 'efiemu64.o', 'backtrace.mod', 'freedos.mod', 'memdisk.mod', 'terminal.mod', 'romfs.mod', 'blocklist.mod', 'jpeg.mod', 'gcry_cast5.m
od', 'keystatus.mod', 'bswap_test.mod', 'testspeed.mod', 'ntfs.mod', 'truecrypt.mod', 'eval.mod', 'macbless.mod', 'test.mod', 'newc.mod', 'tr.mod', 'lsapm.mod', 'extcmd
.mod', 'part_acorn.mod', 'morse.mod', 'legacycfg.mod', 'terminfo.mod', 'vga_text.mod', 'squash4.mod', 'geli.mod', 'cbtime.mod', 'nilfs2.mod', 'minix2.mod', 'progress.mo
d', 'biosdisk.mod']
/boot/grub2/locale [] ['sr.mo', 'ja.mo', 'ca.mo', 'nb.mo', 'sl.mo', 'de@hebrew.mo', 'it.mo', 'en@greek.mo', 'id.mo', 'en.mo', 'zh_TW.mo', 'sv.mo', 'vi.mo', 'de_CH.mo',
'de.mo', 'ru.mo', 'uk.mo', 'pa.mo', 'en@hebrew.mo', 'lt.mo', 'fr.mo', 'en@piglatin.mo', 'pt_BR.mo', 'en@cyrillic.mo', 'eo.mo', 'en@quot.mo', 'fi.mo', 'gl.mo', 'pl.mo',
'nl.mo', 'es.mo', 'hu.mo', 'da.mo', 'en@arabic.mo', 'zh_CN.mo', 'ast.mo', 'tr.mo']
/boot/grub2/themes ['system'] []
/boot/grub2/themes/system [] ['fireworks.png', 'background.png']
/boot/grub2/fonts [] ['unicode.pf2']


>>> dir(os)
['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWA
RE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_OK', 'NGROUPS_MAX', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE'
, 'O_NDELAY', 'O_NOATIME', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'P_NOWAIT', 'P_NOWAITO', 'P_WAIT',
'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'ST_APPEND', 'ST_MANDLOCK', 'ST_NOATIME', 'ST_NODEV', 'ST_NODIRATIME', 'ST_NOEXEC', 'ST_NOSUID', 'ST_RDONLY', 'ST_RELATIME',
'ST_SYNCHRONOUS', 'ST_WRITE', 'TMP_MAX', 'UserDict', 'WCONTINUED', 'WCOREDUMP', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WS
TOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_ex
ists', '_exit', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', '_spawnvef', 'abort', 'access', 'alts
ep', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'ctermid', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno
', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdatasync', 'fdopen', 'fork', 'fork
pty', 'fpathconf', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'getcwd', 'getcwdu', 'getegid', 'getenv', 'geteuid', 'getgid', 'getgroups', 'getloadavg', 'getlogin', 'get
pgid', 'getpgrp', 'getpid', 'getppid', 'getresgid', 'getresuid', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchown', 'linesep', 'link', 'listdir', '
lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names',
'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'setegid', 'seteui
d', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setregid', 'setresgid', 'setresuid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spaw
nv', 'spawnve', 'spawnvp', 'spawnvpe', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'symlink', 'sys', 'sysconf', 'sysconf_names',
'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'w
ait4', 'waitpid', 'walk', 'write']
  1. open() 还有哪些模式可以使用?
    • open()有以下几种模式:
      • 'r': 以只读方式打开已存在文件,若文件不存在则抛出异常。此方式是默认方式
      • 'U'或者'rU': Python惯例构造了通用换行支持;提供'U'模式以文本方式打开一个文件,但是行可能随时结束:Unix的结束符规定为'\n',苹果系统则为'\r',还有Windows规定为'\r\n',所有这些规定在Python程序中统一为'\n'.
      • 'w': 以可写方式打开存在或者不存在的文件,若文件不存在则先新建该文件,若文件存在则覆盖该文件
      • 'a': 用于追加,对unix系统而言,所有的内容都将追加到文件末尾而不管指针的当前位置如何
      • 'b': 以二进制方式打开。打开一个二进制文件必须用该模式。增加'b'模式是用来兼容系统对当二进制和文本文件的处理不同
      • 'r+','w+'和'a+'以更新方式打开文件(注意'w+'覆盖文件)


目录
相关文章
|
算法 Python
第25天:从 0 学习 Python 0-20 天合集总结
第25天:从 0 学习 Python 0-20 天合集总结
112 0
|
16天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
16天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
20天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。
|
1天前
|
机器学习/深度学习 数据挖掘 API
pymc,一个灵活的的 Python 概率编程库!
pymc,一个灵活的的 Python 概率编程库!
4 1
|
1天前
|
人工智能 算法 调度
uvloop,一个强大的 Python 异步IO编程库!
uvloop,一个强大的 Python 异步IO编程库!
10 2
|
2天前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
9 0
|
2天前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践
|
3天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
3天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信