python基础知识

一、python的基础

1、简单写一行python代码:

print(‘hello,world’)
print(‘你好,北京’)

2、变量:

name="刘梦雅"
print(name)

程序交互:

name=input“请输入你的名字”
print(name)

3、数据类型

(1)int,long类型:

a=2*20
print(a,type(a))

(2)字符串类型str:

name=input('请输入你的名字')
print(name,type(name))()

(3)布尔类型:

print(true,type(true))

4、if语句

choice=input(‘请输入你猜的数字’)
if  choice==‘2’
   print(‘我请你吃饭’)
else
 print(‘你请我吃饭’)

5、while语句

 

flag = true;
count =1;
while flag:
    print(count);

6、break语句:

count=1
while  true:
    print(count)
   count=count+1
 if count==101
  break

7、continue语句 

count=0
 while count<10;
  count =count +1
if count ==7;
continue ;
print(count )

8、in   not  in的用法:

comment =input(‘请输入你的评论’)
    if  ‘傻子’ in  comment:
        print (‘您输入的为敏感词汇,请重新输入’)

9、while  else 

复制代码
count = 1
while True:
    print (count )
    if count == 3:break 
       count +=1
else 
    print('循环正常')
复制代码

输出   1

   2

          3

复制代码
count =1
flag = True
while flag :
    print (count)
    if count == 3:
        flag=False
     count+=1
else :
     print ('循环正常完毕')
复制代码

输出:1

   2

     3

   循环正常完毕

10、格式化输出:

1)第一种

name=input(‘请输入你的名字:’)
age  =input(‘请输入你的年龄:’)
hobby=input(‘请输入你的爱好:’)
ms =‘我叫%s,今年%d,爱好是%s’ % (name,int(age),hobby)
print(ms)

2)第二种

dic = {‘name’:'oldboy','age':15,'hobby':'踢足球'}
msg=‘我叫%(name)s,今年%(age)d,爱好是%(hobby)s’ % dic
print(msg)

3)第三种

再格式化输出中单纯的显示% 用%%解决

name=input(‘请输入你的名字:’)
age  =input(‘请输入你的年龄:’)
msg=‘我叫%s,今年%d,学习进度为1%%’ % (name,int(age))
print(msg)

11、运算符

print(int(True))
print (int(False))           输出:1  0   True
print(bool(100))

and   or      not   

print (1   or    3)                          输出   1
print  (1  and 3 )                                  3
print (0   or  2)                                    2
print(0  and 2)                                     0
print  (1>2 or 3 and  4)                       4

 

 

 

 

转载于:https://www.cnblogs.com/hnlmy/p/9520262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值