Python语法

Posted Jin_c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python语法相关的知识,希望对你有一定的参考价值。

import time;
import turtle;

# a = 23423432;b = 2342343452.23452;c = 234234323452.23452;d = 234293432.23452;
# print(format(a,"10X"));
# print(format(b,"<10.2f"));
# print(format(c,"<10.2f"));
# print(format(d,">10.2f"));
#画一个三角形
# turtle.pensize(3);
# turtle.penup();
# turtle.goto(-200,-50);
# turtle.pendown();
# turtle.circle(40, steps = 3);
#画一个四边形
# turtle.penup();
# turtle.goto(-100,-50);
# turtle.pendown();
# turtle.circle(40,steps=4);
#画一个五边形
# turtle.penup();
# turtle.goto(0,-50);
# turtle.pendown();
# turtle.circle(40,steps=5);
#画一个圆
# turtle.penup();
# turtle.goto(100,-50);
# turtle.pendown();
# turtle.circle(40);
#画一个红点
# turtle.penup();
# turtle.goto(200,-50);
# turtle.pendown();
# turtle.dot(6,"red");

#画一个三角形
# turtle.pensize(3);
# turtle.penup();
# turtle.goto(-200,-100);
# turtle.pendown();
# turtle.begin_fill();
# turtle.color("yellow");
# turtle.circle(40, steps = 3);
# turtle.end_fill();
#选择
# number = eval(input("enter a number"));
# if number % 5 == 0:
# print("HIFive");
# print("HIFive");
# else:
# print("HIFive");
#循环
# number = eval(input("enter a number"));
# while number%5 != 0:
# print("wrong number!");
# number+=1;
# initialValue = 0;
# endValue = 9;
# for i in range(initialValue,endValue):
# print(i);
#函数
# def main(n,m):
# print(function2(3.4));
# def function2(n):
# if n > 0:
# return 1;
# elif n == 0:
# return 0;
# elif n < 0:
# return -1;
# main(2,3);
#类
# class ClassName:
# initializer;
# methods();
# import math;
# class Circle:
# def __init__(self,radius =1):
# self.radius = radius;
# def getPerimeter(self):
# return 2 * self.radius*math.pi;
# def getArea(self):
# return self.radius * self.radius * math.pi;
# def setRadious(self,radius):
# self.radius = radius;
# c = Circle(5);
# print(c.radius);
# print(c.getArea());
# print(c.getPerimeter());
#字符串函数
#构造函数穿件字符串
# s1 = str("i have spacestring");
# s2 = str("Welcome");
#用字符串值创建一个字符串
# s3 = "";
# s4 = "jin xueling";
# print(len(s3));
# print(len(s4));
# print(max(s4));
# print(min(s4));
# for i in range(0,len(s4),2):
# print(s4[i],end=‘‘);
# print();
# #python中的负下标
# print(s4[-1]);
# #截取
# print(s4[1:4]);
#连接和复制
# print(s2+s4);
# print(3*s4+"交换律"+s4*3);
#包含
# print("come" in s2);
# print("come" not in s2);
#比较字符串
# print(s2 < s4);
# for ch in s4:
# print(ch);
#字符串特征函数
# print(s4.isalnum());
# s4.isalnum();#atlast one number
# s4.isalpha();#atlast one pharmeter
# s4.isdigit();#all element is number
# s4.isidentifier();#是否是Python标识符
# s4.islower();#all element is lower;
# s4.isupper();#all element is upper;
# s4.isspace();#all element is space;
#搜索字符串
# str = " i am sOuce sTring "
# str.endswith(s1);#判断str尾巴
# str.startswith(s2);#判断str开头
# str.find(s1);#查找str中s1最左边下标
# str.rfind(s1);#查找str中是最右边下标
# str.count(s1);#查找s1出现的次数,s1无覆盖
#转换字符串
# print(str.capitalize());
# print(str.lower());
# print(str.upper());
# print(str.swapcase());
# print(str.replace("g","替换"));
#删除字符串中空格
# print(str.lstrip());
# print(str.rstrip());
# print(str.strip());
#校验回文串
# def main():
# s = input("enter a string").strip();
# if isPalindrome(s):
# print(s,"is a parlindrome");
# else:
# print(s,"is not a parlindrome");
# def isPalindrome(s):
# low = 0;
# high = len(s)-1;
# while low < high:
# if s[low] != s[high]:
# return False;
# low+=1;
# high-=1;
# return True;
# main();
#列表:不同于字符串是字符的序列,列表是任何元素的序列
# list1 = list("abcd");
# list2 = list(range(3,5));
# list3 = list[2,3,4];
# items = "ni hao jin xue ling".split();
# lst = [];
# print("enter 10 numbers");
# for i in range(10):
# lst.append(eval(input()))
#统计文件中的字符个数
def main():
filename ="E:\workspace1\pythonTest\wordFile.txt";
infile = open(filename,"r");
counts = 26*[0];
for line in infile:
countLetters(line.lower(),counts);
for i in range(len(counts)):
if counts[i] != 0:
print(chr(ord("a")+i)+" appears"+str(counts[i])+("time" if counts[i] == 1 else "times"));
infile.close();
def countLetters(line,counts):
for ch in line:
if ch.isalpha():
counts[ord(ch) - ord(‘a‘)] += 1;
main();
























































































































































































以上是关于Python语法的主要内容,如果未能解决你的问题,请参考以下文章

在下面的代码片段中的剩余 ='passthrough' 处的代码中出现语法错误

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

[Python]常用代码块