python 基本语法

# -*- coding: UTF-8 -*-
import numpy as np


#变量
str_test="hell python"
int_test=123
float_test=123.5
print(str_test);print(type(str_test))
print(type(int_test))
print(type(float_test))

输出结果:

hell python
<type 'str'>
<type 'int'>
<type 'float'>


LIST

#List
months=[]
months.append("aaaa")
months.append(2)
months.append(2.5)
months.append("cccc")
print(type(months))
print(months)
print(months[0])
print(len(months))
print(months[1:3])#左包含,右不包含
print(months[1:])#左包含,右不包含
输出:

<class 'list'>
['aaaa', 2, 2.5, 'cccc']
aaaa
4
[2, 2.5]
[2, 2.5, 'cccc']


循环控制

#循环
months1=["aaa","bbb","cc","ddd",2]
for i in months1:
    print(i)

n=0
while n< 3:
    n+=1
    print(n)
#多层循环
months2=[["aaa","bbb","cc","ddd"],[1,2,3,4]]
for month in months2:
    print(month)
for month in months2:
    for i in month:
        print(i)


控制结构 字典

#控制结构  字典
if True:
    print(True)
else:
    print(False)
dic ={}
dic1 ={
    "eee":555,
    "fff":666
}
print(dic1)
print(type(dic))
dic["aaa"]="111";dic["bbb"]="222";dic["ccc"]="333";dic["bddd"]="444";
keys=dic.keys()
print(dic.keys())
for key in keys:
    print(dic[key])
输出:
True
{'eee': 555, 'fff': 666}
<class 'dict'>
dict_keys(['aaa', 'bbb', 'ccc', 'bddd'])
111
222
333
444

WORDCOUNT

def wordConut():
    list1=["aa","aa","xx","xx","xx","xx"]
    dic2={}
    for word in list1:
        if(word in dic2):
            dic2[word]=dic2[word]+1
        else :
            dic2[word]=1
    print(dic2)

wordConut()
输出:

{'aa': 2, 'xx': 4}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值