Python手记

字符串的拼接

1.“+”,如果是字符和数字相连,要使用str()函数对于数字进行字符转化;

2.join()

3.",",链接的两个字符串之间用空格做关联

4.占位符

tmp +=1

#print ‘r‘%tmp

print("row num is: %s"%(tmp))

print(‘value is: %s; RowNum is: %s‘%(cellValue,rowNo)) #多个占位

python的全局变量

第一种是def consts()中进行定义(函数名自定义),在使用时记得首先要consts()一下。

def consts():

global rowNum

consts()

print(str(rowNum))

def test():

global rowNum

rowNum=rowNum+1

print(rowNum)

这里注意在常量函数中声明全局变量的时候,要使用global,如果在另外一个函数里面要使用,而且需要修改,也要声明global,告知编译器我修改的是全局变量,否则会报错;但是在主函数中使用则不需要。

第二种是定义一个外部文件,再import 进来

时间: 2024-08-29 01:00:00

Python手记的相关文章

Python 手记

Python 手记 python 2.7 notepid ++ 编辑器 <简明 Python 教程> www.byteofpython.info 获得人生中的成功需要的专注与坚持不懈多过天才与机会. ——C.W. Wendte wxPython.Twisted和Python图像库 Python.org/download 点击控制面板->系统->高级->环境变量.在“系统变量”表单中点击叫做PATH的变量,然后编辑这个变量,把;C:\Python23加到它的结尾.当然,是Pyt

菜鸟教程 Python 手记 1

Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. 编码 编码默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- 多行语句 Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句,例如: total = item_one + item_two + item_three 在 [], {}, 或 () 中

Python 手记-3

1.PyCharm配置模板 file------settings-----editer-----filie*--------templates------python script 添加 #!/usr/bin/env python # -*- coding:utf-8 -*- #Writed by liyang 这样,创建同样文件的时候,会自动添加这些内容到新建文件 2.小练习:生成字典 文件内容: alex|123|1 eric|123|1 emmy|123|1 obj = file('log

python手记(7)------字典(操作方法)

1.dict方法概述 In[70]: dir(dict) Out[69]: ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ite

python手记(52)

import  sys import pygamefrom pygame.locals import *import timepygame.init()screen=pygame.display.set_mode((500,550))myfont=pygame.font.Font(None,30)red=(255,0,0)green=(0,255,0)textImage=myfont.render(time.strftime("%Y-%m-%d  %H:%M:%S", time.loc

python手记(53)

import sys import pygame from pygame.locals import * import time import math pygame.init() screen=pygame.display.set_mode((500,550)) myfont=pygame.font.Font(None,30) red=(255,0,0) green=(0,255,0) blue=(0,0,255) pygame.display.set_caption("myhaspl&quo

python手记-twisted(2)

关闭连接 Xshell 5 (Build 0655)Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `help' to learn how to use Xshell prompt.[c:\~]$ telnet 120.55.*.* 8001 Connecting to 120.55.*.*:8001...Connection established.To escape to local she

python手记(11)------while循环(break 和 continue)

1.while 比 for 适用性更广:满足条件情况下一直进行 2.猜数字游戏1.0版 import numpy as np ''' 猜整数1.0版: 1.记录次数 2.给出范围 3.while语句,包括continue.break.tyr.except''' answer=np.random.randint(1,101) i=0 while True: try: guess=int(input('请猜一个1-100的正整数:')) i+=1 if guess==answer: print('太

python手记(3)------字符串

1.字符串格式化----% 原来的%: In[2]: a='my name is %s and my age is %d'%('eric',26) In[3]: a Out[3]: 'my name is eric and my age is 26' 占位符%:为真实值预留位置,并控制显示的格式.占位符可以包含有一个类型码,用以控制显示的类型,常用如下: %s    字符串 (采用str()的显示) %r    字符串 (采用repr()的显示) %c    单个字符 %b    二进制整数 %