python基础学习

hello world:

message_1 = "hello python"
message_2 ="world!"
num=2333 #no chinese  this is zhu shi
#no chinese
message_full=message_1+" "+message_2+str(num)
print(message_full.title())

list:

EDG=['meiko','scout','zet',"deft"]
print(EDG)
del EDG[3]
print(EDG)
EDG.append("mouse")
print(EDG)
EDG.insert(1,"clearlove")
print(EDG)
print(EDG[0].title())
print(EDG[-1].title())
message="i think "+EDG[0]+" is the best sup."
print(message.title())
EDG.sort()
print(EDG)
print(len(EDG))

for:

sups=["meiko","five","mata","ming"]
for sup in sups:
   print(sup+" is god")
print("But i think meiko is the best")

for value in range(1,5):
   print(value)

numbers=list(range(1,10,2))
print(numbers)

squares=[value**3 for value in range(1,11)]
print(squares[0:4])

if:

car="Bmw"
if car.lower() == "bmw":
    print("true")
else:
    print("false")

print(car=="bmw")

else if:

users=["admin","rsin","zhjf","7520"]

if users:
    for user in users:
        if user=="admin":
            print("Hello admin,would you like to see a status report?")
        else:
            print("Hello Eric,think you for logging in again.")
else:
    print("there is no user")

zidain:

friend ={"name":"chenqiyu","nikename":"xiaoyu","age":20,"city":"beijing"}
print(friend)
print("not beijing,he is in nanjing")
friend["city"]="nanjing"
friend["class"]=10
print(friend)
del friend["class"]
print(friend)
print(friend["name"]+"("+friend["nikename"]+")"+" in "+friend["city"]+" is "+str(friend["age"])+" years old.")
print("*****************************************")
for key,value in friend.items():
    if key!="age":
        print(key+":"+value)
    else:
        print(key+":"+str(value))
print("*****************************************")
for key in friend.keys():
    print(key)
print("*****************************************")
for value in friend.values():
    print(value)
print("*****************************************")
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':10}
alien_2={'color':'red','points':15}
aliens=[alien_0,alien_1,alien_2]
for alien in aliens:
    print(alien)

input:

age=input("plz input your age but not in 18~50:")
age=int(age)

while age>=18 and age<=50:
    age=input("plz input your age but not in 18~50:")
    age=int(age)

if age<18:
    print("you are young")
elif age>50:
    print("you are old")

number=0
while number<10:
    number+=1
    if number%2 == 1:
        continue

    print(number)

function:

def sayhello():
    """show hello"""
    print("hello")

def toyou(name="???"):
    """say hello to you"""
    print("hello "+name)

def make_shirt(cm,cha="rsin"):
    print("the shirt with "+cha+" is "+str(cm))

def get_plus(a,b):
    c=a+b
    return c

def get_users(names):
    for name in names:
        msg="hello, "+name.title()+"!"
        print(msg)

usernames=["hannah","ty","margot"]
get_users(usernames)

sayhello()
name=input("plz input your name:")
toyou(name)
cm=input("plz input your cm:")
cm=int(cm)
make_shirt(cm,name)

a=input("plz input a:")
a=int(a)
b=input("plz input b:")
b=int(b)
c=get_plus(a,b)
print(str(c))

class:

class dog():
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def sit(self):
        print(self.name.title()+" is sitting now")
    def roll_over(self):
        print(self.name.title()+" is rolled now")

class cutedog(dog):
    def __init__(self,name,age):
        super().__init__(name,age)

my_dog=cutedog('wufan',2)
print(my_dog.name.title())
print(str(my_dog.age))
my_dog.sit()
my_dog.roll_over()

file:

with open('D:\pi_digits.txt') as file_object:
    contents=file_object.read()
    print(contents)

print("*******************")

with open('D:\pi_digits.txt') as file_object:
    for line in file_object:
        print(line.rstrip())

print("*******************")

with open("D:\pi_digits.txt") as file_object:
    lines=file_object.readlines()

pi_string=''
for line in lines:
    pi_string+=line.strip()
print(pi_string)

with open('ok.txt','a') as file_obj:
    file_obj.write("hello boys")

dump:

import json
numbers=[2,3,5,7,11,13]
filename='numbers.json'
with open(filename,'w') as f_obj:
    json.dump(numbers,f_obj)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值