码迷,mamicode.com
首页 > 编程语言 > 详细

python学习第一篇

时间:2018-03-04 22:12:25      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:mat   org   数据   字符串   inf   shandong   hello   star   分享图片   

一:python安装及开发工具(pycharm)安装

  1.在python官网(https://www.python.org/)安装最新版的python文件。

  2.下载pycharm最新版并安装(www.jetbrains.com)

二:第一个python程序(Hello World)

  

print(hello World)

 

三:变量

  1.变量声明:

技术分享图片
1 name = jone
2 name2="jone"#字符变量要用‘xx‘或者"xx"
3 num = 1
View Code

   2.输出变量:

1 print(name,name2)
2 #或者print(name+name2)

四:用户交互式输入

1 name = input(name:)
2 print("Hello",name)

显示name:提示用户输入数据,假设为‘jone’将jone赋值给name这个变量

五:格式化输出

1.

 1 name = jone
 2 age = 18
 3 address = shandong
 4 info =‘‘‘
 5     ------‘‘‘+name+‘‘‘-------
 6     name:‘‘‘+name+‘‘‘
 7     age:‘‘‘+age+‘‘‘
 8     address:‘‘‘+address+‘‘‘
 9     ‘‘‘
10 print(info)

不推荐使用该方法且只能食醋出字符串,不能输出int型

2.

 1 name = jone
 2 age = 18
 3 address = shandong
 4 info =‘‘‘
 5     ------%s-------
 6     name:%s
 7     age:%d
 8     address:%s
 9     ‘‘‘%(name,name,age,address)
10 print(info)

每一个小括号里的值对应一个格式输出的值

3.

 1 name = jone
 2 age = 18
 3 address = shandong
 4 info =‘‘‘
 5     ------{name1}-------
 6     name:{name1}
 7     age:{age1}
 8     address:{address1}
 9     ‘‘‘.format(name1 = name,
10         age1 = age,
11         address1 = address)
12 print(info)

4.

name = jone
age = 18
address = shandong
info =‘‘‘
    ------{0}-------
    name:{0}
    age:{1}
    address:{2}
    ‘‘‘.format(name,
        age,
        address)
print(info)

 

六:if...语句

1 #猜年龄
2 age_of_me = 20
3 age =int(input(guess age:))
4 if age == age_of_me:
5     print(get it)
6 elif age > age_of_me:
7     print(too old)
8 else:print(too young)

 

七:while循环

 1 #猜年龄
 2 age_of_me = 20
 3 i = 0
 4 while i<3:
 5     age = int(input(guess age:))
 6     if age == age_of_me:
 7         print(get it)
 8         break
 9     elif age > age_of_me:
10         print(too old)
11         i += 1
12         continue
13     else:
14         print(too young)
15         i += 1
16 else:
17     print(game over)    #只有循环正常结束才会运行,被berak中断的不会执行

 

八:for循环:

1.

for i in range(10):
    print(i)

补充:range函数range(1,2,3)        * range函数是一个用来创建算数级数序列的通用函数,返回一个[start, start + step, start + 2 * step, ...]结构的整
                                                                  数序列

  1.开始值,缺省默认为0

  2.结束值,不可缺省

  3.每个x个数返回一个

2.猜年龄

 1 #猜年龄
 2 age_of_me = 20
 3 i = 0
 4 for i in range(3):
 5     age = int(input(guess age:))
 6     if age == age_of_me:
 7         print(get it)
 8         break
 9     elif age > age_of_me:
10         print(too old)
11         i += 1
12         continue
13     else:
14         print(too young)
15         i += 1
16 else:
17     print(game over)      #只有循环正常结束才会运行,被berak中断的不会执行

 

python学习第一篇

标签:mat   org   数据   字符串   inf   shandong   hello   star   分享图片   

原文地址:https://www.cnblogs.com/fthpython/p/8505322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!