- 输出语句
-
x = 1 y = 2 z = 3 print("the values are", x, y, z) #打印结果为 the values are 1 2 3
-
-
unsigned short hex=0x1234;//定义一个2字节整数 unsigned char low = hex & 0xff; //取低8位 也就是0x34 unsigned char hight = hex >> 8; //取高8位 也就是0x12 printf("low=0x%x,hight=0x%x\n",low,hight);//打印结果就是low=0x34,hight=0x12
- 取二进制数的某一位
-
1.首先把1往左移3位:(1的二进制是1) int c=1<<3;//此时c的二进制为1000,是1左移三位的结果 1 2.然后把c和a作与运算,所得结果就能告诉你答案 int d=a&c;//1010&1000=1000,那么d的结果的二进制就是1000,此时若a的第3位为0,那么d会等于0
-
- 字典
-
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Name']: ", dict['Name'] print "dict['Age']: ", dict['Age']
- 字典
- 将字典作为参数传入一个函数的时候只需要传入字典名称就可以了
-
def func(d): for key in d: print("key:", key, "Value:", d[key]) # Driver's code D = {'a':1, 'b':2, 'c':3} func(D)
-
- python中的类
- switch语句
- switch语句
-
def vowel(num): switch={ 1:'a', 2:'e', 3:'i', 4:'o', 5:'u' } return switch.get(num,"Invalid input") vowel(3) vowel(0)
- pass 关键字
- pass无任何实际意义,可以在空函数中写pass
- pass关键字的链接
-
for letter in 'Python': if letter == 'h': pass print 'This is pass block' print 'Current Letter :', letter print "Good bye!" Current Letter : P Current Letter : y Current Letter : t This is pass block Current Letter : h Current Letter : o Current Letter : n Good bye!
- 逻辑运算符
- 且:and
-
a = 10 b = 10 c = -10 if a > 0 and b > 0: print("The numbers are greater than 0") if a > 0 and b > 0 and c > 0: print("The numbers are greater than 0") else: print("Atleast one number is not greater than 0")
-
- 链接
- 且:and
- for 循环
- for i in range(12)表示0到11
- for i in range(3, 6)表示3,4,5
- 相关链接
- python中打开文件
- 函数open函数或者with open语句,一般使用with open语句,因为他简化了对文件的操作
-
$ cat output.txt Line1 Line2 Line3 Line4 Line5
-
with open('output.txt', 'r+') as f: for line in f: print(line, end='') [output] $ python with_open_example.py Line1 Line2 Line3 Line4 Line5
- 相关链接
- python中读文件的语句
- readline()
- 用来读取文件中的完整一行,可以传入字节的数目作为参数,表示读取几个字节
-
txt文件 Python is the best programming language in the world in 2020 Edureka is the biggest Ed-tech platform to learn python Python programming is as easy as writing a program in simple English language
file = open("example.txt", "r") example1 = file.readline() example2 = file.readline(14) print(example1) print(example2) 输出为 Python is the best programming language in the world in 2020 Edureka is the
- readlines()
- 用来读取整个文件,可以传入字节数目作为参数
-
example1 = file.readlines() print(example1) 输出为 ['Python is the best programming language in the world in 2020', 'Edureka is the biggest Ed-tech platform to learn python', 'Python programming is as easy as writing a program in simple English language'] example2 = file.readlines(80) print(example2) 输出为 ['Python is the best programming language in the world in 2020', 'Edureka is the biggest Ed-tech platform to learn python']
- 相关链接
- readline()
- locals()函数
- 作用:返回当前函数正在使用的符号表
- locals 以字典形式返回局部变量,globals 以字典形式返回全局变量
- 相关链接
- 例
-
def localsNotPresent(): return locals() def localsPresent(): present = True return locals() print('localsNotPresent:', localsNotPresent()) print('localsPresent:', localsPresent()) 输出为 localsNotPresent: {} localsPresent: {'present': True}
-
- 字符串的长度len()
-
str = "this is string example....wow!!!" print ("Length of the string: ", len(str)) 输出为 Length of the string: 32
-
- split函数
- split函数用来返回字符串中所有的单词
- 如果无参数则根据空格和换行符进行分割,如果有参数则根据参数进行分割
-
str = "this is string example....wow!!!" print (str.split( )) print (str.split('i',1)) print (str.split('w')) 输出语句 ['this', 'is', 'string', 'example....wow!!!'] ['th', 's is string example....wow!!!'] ['this is string example....', 'o', '!!!']
- 相关链接
- list和array
- 区别
- list
- 长度可变,动态大小
- 元素类型不定
- array
- 长度固定
- 元素类型固定
- 相关链接
- list
- list
- 相关链接
- 下标
- 下标为负数表示从最后一个元素开始的下标
- 冒号用来表示下标的范围
-
# Creating a List List = ['G','E','E','K','S','F', 'O','R','G','E','E','K','S'] print("Initial List: ") print(List) # Print elements from beginning # to a pre-defined point using Slice Sliced_List = List[:-6] print("\nElements sliced till 6th element from last: ") print(Sliced_List) # Print elements of a range # using negative index List slicing Sliced_List = List[-6:-1] print("\nElements sliced from index -6 to -1") print(Sliced_List) # Printing elements in reverse # using Slice operation Sliced_List = List[::-1] print("\nPrinting List in reverse: ") print(Sliced_List) 输出 Initial List: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S'] Elements sliced till 6th element from last: ['G', 'E', 'E', 'K', 'S', 'F', 'O'] Elements sliced from index -6 to -1 ['R', 'G', 'E', 'E', 'K'] Printing List in reverse: ['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']
- 区别
- 注释
- 单行注释
- #
- 多行注释
- """ comment """
- 三个双引号
- 单行注释
python基础
最新推荐文章于 2023-08-24 22:16:45 发布