您现在的位置是:首页 > 正文

python3基础语法速览——笨方法学python3读书笔记

2024-01-30 22:34:27阅读 0
utf-8编码

# --coding: utf-8--
打印

print("hello world")
print("Hens", 25 + 30 / 6)
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("Is it greater ?", 5 >= -2)
print("It's fleece was white as %s." % 'snow')
print("." * 10)

formatter = "%s %s %s %ss"
print(formatter %(1, 2, 3, 4))
print(formatter %("one", "two", "three", "four"))
print(formatter %(True, False, False, True))
print(formatter %(formatter, formatter, formatter, formatter))
print(formatter %("I had this thing", "that you could", "but it did", "so i said"))

print(
"""
there is something going on here.
with the three double-quotes.
we will be able to type as much as we like
even 4 line if we want, or 5, or 6.
"""
输入

print("how old are you?", end=' '),
age = input()

age = input("How old are you? ")
参数

from sys import argv

script, first, second, third = argv

print("script is: ", script)
print("1st var is: ", first)
print("2nd var is: ", second)
print("3rd var is: ", third)
读写文件

# 读取
from sys import argv

script, filename = argv

txt = open(filename)

print("here is you file %r" % filename)
print(txt.read())
txt.close()

# 读写
from sys import argv

script, filename = argv

print("we are going to erase %r." % filename)
print("if you don't want that, hit ctrl + c.")
print("if you do want that, hit enter")

input("?")

print("opening the file")
target = open(filename, 'w')

print("truncating the file. goodbye!")
target.truncate()

print("now i am going to ask you for three lines.")

line1 = input("1: ")
line2 = input("2: ")
line3 = input("3: ")

print("i am going to write these lines to file.")

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("finally, we close it")
target.close()

# 文件复制
from sys import argv
from os.path import exists

script, from_file, to_file = argv

print("copying from %s to %s" %(from_file, to_file))

raw_input = open(from_file)
indata = raw_input.read()

print("the input file is %d bytes long" % len(indata))
print("does the output file exist? %r " % exists(to_file))
print("ready, hit enter to continue, ctrl+c to abort.")
input()

output = open(to_file, 'w')
output.write(indata)

print("ok, all done")

output.close()
raw_input.close()
定义函数

# 简单函数
def print_two(*args):
	arg1, arg2 = args
	print("arg1: %r, arg2: %r" %(arg1, arg2))
	
def print_two_again(arg1, arg2):
	print("arg1: %r, arg2: %r" % (arg1, arg2))
	
def print_one(arg1):
	print("arg1: %r" % arg1)
	
def print_none():
	print("i got nothing")
	
print_two("zed", "shaw")
print_two_again("zed", "shaw")
print_one("first")
print_none()

# 实现算术运算
def add(a, b):
	print("add %d + %d" %(a, b))
	return a + b
	
def sub(a, b):
	print("sub %d - %d" % (a, b))
	return a - b
	
def mul(a, b):
	print("mul %d * %d" % (a, b))
	return a * b
	
def div(a, b):
	print("div %d / %d" %(a, b))
	return a / b
	
print("let's do some math with just functions")

age = add(30, 5)
height = sub(78, 4)
weight = mul(30, 2)
iq = div(100, 2)

print("values: ", age, height, weight, iq)

print("here is a puzzle")
what = add(age, sub(height, mul(weight, div(iq, 2))))

print("values: ", what, " you right?")
分支

people = 20
cats = 30
dogs = 15

if people < cats:
	print("too many cats")
	
if people > cats:
	print("not too many cats")
	
if people < dogs:
	print("too many dogs")
	
if people > dogs:
	print("ok")
	
dogs += 5

if people >= dogs:
	print("people are greater than equal to dogs")
	
if people <= dogs:
	print("people are less than or equal to dogs")
	
if people == dogs:
	print("people are dogs")
循环

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

for number in the_count:
	print("this is count %d" % number)
	
for fruit in fruits:
	print("a fruit of type: %s" % fruit)
	
for i in change:
	print("i got %r" % i)
	
elements = []

for i in range(0, 6):
	print("adding %d to the list." % i)
	elements.append(i)
	
for i in elements:
	print("element was: %d" %i)

网站文章

  • java反射机制的应用场景有哪些?

    反射是框架设计的灵魂。 在我们平时的项目开发过程中,基本上很少会直接使用到反射机制,但这不能说明反射机制没有用,实际上有很多设计、开发都与反射机制有关,例如模块化的开发,通过反射去调用对应的字节码;动...

    2024-01-30 22:34:17
  • kali中的一些工具简单使用dirb、netdiscover、ffuf、nmap、sqlmap、hydra、msfconsole

    kali中的一些工具简单使用dirb、netdiscover、ffuf、nmap、sqlmap、hydra、msfconsole

    Netdiscover是一种网络扫描工具,通过ARP扫描发现活动主机,可以通过主动和被动两种模式进行ARP扫描。它主要为那些没有dhcp服务器的无线网络开发,当您进行Wardriving时。它也可以用...

    2024-01-30 22:34:10
  • c++ for语句 for循环

    c++ for语句 for循环

    for语句 for语句或者是for循环是最常见的也是复杂的一种循环。 在熟练掌握其语法后,对于一般类似与打印1到N的数字的程序我们都可以快速的写出。 for 语句的基本用法 #include usin...

    2024-01-30 22:33:41
  • HAWQ技术解析(四) —— 启动停止

    HAWQ技术解析(四) —— 启动停止

    前面已经完成了HAWQ的安装部署,也了解了HAWQ的系统架构与主要组件,下面开始使用它。HAWQ作为Hadoop上的一个服务提供给用户,与其它所有服务一样,最基本的操作就是启动、停止、重启服务。要完成...

    2024-01-30 22:33:32
  • CentOS8安装部署cobbler

    CentOS8安装部署cobbler

    CentOS8安装部署cobller 手动安装与自动安装

    2024-01-30 22:33:22
  • linux openssl简单的介绍与实例讲解

    OpenSSL简介SSL是Secure Socket Layer(安全套接层协议)的缩写,可以在Internet上提供秘密性传输。Netscape公司在推出第一个Web浏览器的同时,提出了SSL协议标准,目前已有3.0版本。SSL采用公开密钥技术。其目标是保证两个应用间通信的保密性和可靠性,可在服务器端和用户端同时实现支持。目前,利用公开密钥技术的SSL协议,已成为Inter...

    2024-01-30 22:32:52
  • 搭建自己的 new bing网站----nginx反代(群晖,全docker版)

    搭建自己的 new bing网站----nginx反代(群晖,全docker版)

    本篇教程全萌新向,即便新手也可以看懂演示效果如图。本篇文章以nginx反代为题带来一次new bing 的反代实践,可以搭建在自己的服务器上自用,使用起来更方便一点。go-proxy-bing bin...

    2024-01-30 22:32:45
  • 【Java可执行命令】(十六)诊断命令请求发送工具 jcmd:提供一种简单而强大的方式来管理和监控 Java 进程 ~ 最新发布

    【Java可执行命令】(十六)诊断命令请求发送工具 jcmd:提供一种简单而强大的方式来管理和监控 Java 进程 ~ 最新发布

    jcmd是一个强大且方便的命令行工具,用于与正在运行的 Java 进程进行交互、管理和监控。通过它,可以执行各种诊断命令以了解应用程序的状态,进行调试和性能优化,并提供简洁易用的命令行界面。然而,在使用jcmd时需要注意安全性问题,并且目前仅支持本地 Java 进程的操作。无论是开发人员还是运维人员,都可以从jcmd中获益,并提高对 Java 应用程序的管理和监控能力。

    2024-01-30 22:32:37
  • python获取当前星期几

    python获取当前星期几

    2024-01-30 22:32:08
  • 【Flutter实战 --- 渐变 圆角矩形背景 圆球背景】

    【Flutter实战 --- 渐变 圆角矩形背景 圆球背景】

    需求: 渐变圆球 圆角矩形 单行文本: 多行文本: 关键 圆球: decoration: BoxDecoration 圆角矩形:ClipRRect borderRadius 控制圆角 代码 return Column( children: [ Row( children: [ Padding( padding: const Edge

    2024-01-30 22:32:06