【Python】学习笔记

Pycharm 快捷键

运行:shift+f10
注释:ctrl+/

基本数据类型

Python: Number

var = 10
print(var) #check value

Python: String

str = 'thisIsaString';
print(s[0])	#print the first char in String s;
print(s[1:3])	#print string from index 1 to index 3(not include index 3)

Python: List

alist = [ 'runoob', 786 , 2.23, 'john', 70.2 ]	
#Note: for non-number str, '' is a must
print(alist[1])	#print the second element in alist;
alist.append(jim)	#add element at the end of list
del alsit[0]		#delete the first element
#list.insert(index, obj)
alist.insert(0,'hali') #insert 'hali' at the first position

Python: tuple
Difference from list, the value of tuple can’t be changed.

atuple = ( 'runoob', 786 , 2.23, 'john', 70.2 );
print(atuple[1])

Python: dictionary

#d = {key1 : value1, key2 : value2 }
#值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
#字典值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的,但键不行。
tinydict = {'name': 'runoob', 'code': 6734, 'dept': 'sales'}
print(tinydict.values())
for key in tinydict.keys():
    print(key)
    print(tinydict[key])
    

Python: 数据类型转换

if-else

a = 1
b = 2
if a == 1 and b ==2:
    print('case1')
elif a == 2 or b == 3:
    print('case2')
else:
    print('case3')

Loop statement

控制语句

break  #终止所在的loop
continue	#跳出本次循环,继续执行下次循环

while loop

count = 0
while count < 9:
    print(count)
    count = count + 1
else:
	print('end')	#while结束时执行else

for loop

  1. 元素遍历迭代
for iterating_var in sequence:
	statements(s)	
#basic grammar, interating_var将遍历sequence中所有元素
for letter in 'Python':  # 第一个实例
    print(letter)

fruits = ['banana', 'apple', 'mango']
for fruit in fruits:  # 第二个实例
    print(fruit)
  1. 通过序列索引迭代
fruits = ['banana', 'apple',  'mango']
print(len(fruits))
print(range(len(fruits)))
for index in range(len(fruits)):#range(3) equals to range(0,3)
    print('fruit'+str(index), fruits[index])
#equal expression
for index in range(0, 3):
    print('fruit', index, fruits[index])

python函数

def functionname( parameters ):	#standard form
   function_suite
   return [expression]
def printme(astring):
    print(astring)
    return
printme('Call function printme')
there are different type of parameter type.

python 模块Module

1个python module就是一个py文件;
模块能定义函数,类和变量,模块里也能包含可执行的代码。
模块的导入:import module1 or from module1 import *
调用模块中的函数:模块名.函数名

python 文件I/O

读取键盘

str1 = input("请输入:")
print("你输入的内容是: ", str1)

文件读写

fo = open('test_data.txt', 'w')
print(fo.name)
fo.write('the_first_element')
fo.close()

fo = open('test_data_1.txt', 'r+')
str1 = fo.read(10)
str2 = fo.readline()
fo.close()
print(str1)
print(str2)

python class/object

class Employee:
    empCount = 0

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1

    def display_count(self):
        print('Total Employee number:', Employee.empCount)

    def display_employee(self):
        print('Total Employee number:')
        print('Name:', self.name, 'Salary:', self.salary)


emp1 = Employee('ali', 10)
emp2 = Employee('cat', 20)
emp1.display_count()
emp1.display_employee()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值