import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user="yourusername", # 数据库用户名 passwd="yourpassword" # 数据库密码 ) print(mydb) import mysql.connector mydb = mysql.connector.connect( host="lo…
import time; # 引入time模块 ticks = time.time() print ("当前时间戳为:", ticks) import time localtime = time.localtime(time.time()) print ("本地时间为 :", localtime) import time localtime = time.asctime( time.localtime(time.time()) ) print ("本地时间…
var1 = 'Hello World!' var2 = "Runoob" #!/usr/bin/python3 var1 = 'Hello World!' var2 = "Runoob" print ("var1[0]: ", var1[0]) print ("var2[1:5]: ", var2[1:5]) #!/usr/bin/python3 var1 = 'Hello World!' print ("已更新字…
print ("abs(-40) : ", abs(-40)) print ("abs(100.10) : ", abs(100.10)) #!/usr/bin/python3 import math # 导入 math 模块 print ("math.ceil(-45.17) : ", math.ceil(-45.17)) print ("math.ceil(100.12) : ", math.ceil(100.12)) p…
#!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("1 - c 的值为:", c) c = a - b print ("2 - c 的值为:", c) c = a * b print ("3 - c 的值为:", c) c = a / b print ("4 - c 的值为:", c) c = a % b print ("5 - c 的值为:",…
#!/usr/bin/python3 counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = "runoob" # 字符串 print (counter) print (miles) print (name) #!/usr/bin/python3 str = 'Runoob' print (str) # 输出字符串 print (str[0:-1]) # 输出第一个到倒数第二个的所有字符 print (str[0]) # 输出字符串第一个字…
#!/usr/bin/python3 # 第一个注释 print ("Hello, Python!") # 第二个注释 #!/usr/bin/python3 # 第一个注释 # 第二个注释 ''' 第三注释 第四注释 ''' """ 第五注释 第六注释 """ print ("Hello, Python!") if True: print ("True") else: print (&q…
def 函数名(参数列表): 函数体 # 计算面积函数 def area(width, height): return width * height def print_welcome(name): print("Welcome", name) print_welcome("Runoob") w = 4 h = 5 print("width =", w, " height =", h, " area =",…
tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5]) tup1 = (12, 34.56); tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的. # tup1[0] = 100 # 创建一个新的元组 tup3 = tup1 + tu…
list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]; list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) pr…
class MyClass: """一个简单的类实例""" i = 12345 def f(self): return 'hello world' # 实例化类 x = MyClass() # 访问类的属性和方法 print("MyClass 类的属性 i 为:", x.i) print("MyClass 类的方法 f 输出为:", x.f()) def __init__(self): self.data…
import os, sys # 假定 /tmp/foo.txt 文件存在,并有读写权限 ret = os.access("/tmp/foo.txt", os.F_OK) print ("F_OK - 返回值 %s"% ret) ret = os.access("/tmp/foo.txt", os.R_OK) print ("R_OK - 返回值 %s"% ret) ret = os.access("/tmp/foo…
# 打开文件 fo = open("runoob.txt", "wb") print("文件名为: ", fo.name) # 关闭文件 fo.close() # 打开文件 fo = open("runoob.txt", "wb") print ("文件名为: ", fo.name) # 刷新缓冲区 fo.flush() # 关闭文件 fo.close() # 打开文件 fo = ope…
str = input("请输入:"); print ("你输入的内容是: ", str) # 打开一个文件 f = open("/tmp/foo.txt", "w") f.write( "Python 是一个非常好的语言.\n是的,的确非常好!!\n" ) # 关闭打开的文件 f.close() # 打开一个文件 f = open("/tmp/foo.txt", "r&quo…
list=[1,2,3,4] it = iter(list) # 创建迭代器对象 for x in it: print (x, end=" ") import sys # 引入 sys 模块 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 while True: try: print (next(it)) except StopIteration: sys.exit() class MyNumbers: def __iter__(self): self…
n = 100 sum = 0 counter = 1 while counter <= n: sum = sum + counter counter += 1 print("1 到 %d 之和为: %d" % (n,sum)) var = 1 while var == 1 : # 表达式永远为 true num = int(input("输入一个数字 :")) print ("你输入的数字是: ", num) print ("G…
if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3 var1 = 100 if var1: print ("1 - if 表达式条件为 true") print (var1) var2 = 0 if var2: print ("2 - if 表达式条件为 true") print (var2) print ("Good b…
fruits = {"apple", "banana", "cherry"} fruits.add("orange") print(fruits) fruits = {"apple", "banana", "cherry"} fruits.clear() print(fruits) fruits = {"apple", "banana&q…
dict = {'} dict1 = { 'abc': 456 } dict2 = { 'abc': 123, 98.6: 37 } dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age']) dict = {'Name': 'Runoob', 'Age': 7, 'C…
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/') dblist = myclient.list_database_names() # dblist = mycl…
import json # Python 字典类型转换为 JSON 对象 data = { 'no' : 1, 'name' : 'Runoob', 'url' : 'http://www.runoob.com' } json_str = json.dumps(data) print ("Python 原始数据:", repr(data)) print ("JSON 对象:", json_str) import json # Python 字典类型转换为 JSON…
<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars&…
import _thread import time # 为线程定义一个函数 def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print ("%s: %s" % ( threadName, time.ctime(time.time()) )) # 创建两个线程 try: _thread.start_new_thread( print_time,…
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options] import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'from@runoob…
# 导入 socket.sys 模块 import socket import sys # 创建 socket 对象 serversocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM) # 获取本地主机名 host = socket.gethostname() port = 9999 # 绑定端口号 serversocket.bind((host, port)) # 设置最大连接数,超过后排队 serversocket.listen…
import pymysql # 打开数据库连接 db = pymysql.connect("localhost","testuser","test123","TESTDB" ) # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 使用 execute() 方法执行 SQL 查询 cursor.execute("SELECT VERSION()")…
<Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI Order allow,deny Allow from all </Directory> print ("Content-type:text/html") print () # 空行,告诉服务器结束头部 print ('<html>') print ('<head>') print ('…
import re print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配 print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配 import re line = "Cats are smarter than dogs" # .* 表示任意匹配除换行符(\n.\r)之外的任何单个或多个字符 matchObj = re.match( r'(.*) are (.*?) .*', l…
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <script> var d=new Date(); document.write(d); </script> </body> </html> &…
import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date 对象 Date date = new Date(); // 使用 toString() 函数显示日期时间 System.out.println(date.toString()); } } import java.util.*; import java.text.*; public class Dat…