一、第一个程序Hello World:

1、打印输出Hello World:

Python2打印方法:

>>> print "hello world"
hello world

Python3打印方法:

>>> print("hello world")

hello world

注:Python3与Pytho2的区别是加了小括号。

2、以文件形式执行代码:

[root@Centos-01 s1]# vim hello.py
打开一个文件hello.py文件内写入以下内容:

print("hello world")

保存退出。

当我们执行代码文件时会出现如下错误:

[root@Centos-01 s1]# ./hello.py
./hello.py: line 1: syntax error near unexpected token `"hello world"'
./hello.py: line 1: `print("hello world")'

出现此错误的原因是系统不知道用什么语言来执行文件内的代码。

解决方法就是用以下命令执行:

[root@Centos-01 s1]# python hello.py
hello world

这样就会正常执行了。但是这样执行会很不方便。我们可以在代码文件中指定用什么语言来执行代码。

将hello.py文件内容修改成如下:

第一种方法:

#!/usr/bin/python
print("hello world")

第二种方法:

#!/usr/bin/env python
print("hello world")

注:第一种方法是指定执行代码语言绝对路径。第二种方法是查找指定的语言。推荐使用第二种方法,因为这样可以提高代码的可移植性。

此时我们再来执行代码:

[root@Centos-01 s1]# python hello.py
hello world
[root@Centos-01 s1]# ./hello.py
hello world

OK,两种方法都可以执行了。

二、变量声明:

info = “hello world”

变量命名规则:

1、变量名只能以数字、字母、下划线组成。

2、第一个字符只能为字母、下划线但不能为数字。

3、不要使用内置变量名。内置变量就是python语言自身内部定义的变量名例如:type、input、impot等。

三、用户交互:

1、用户输入代码:

Python3.X代码:

input_name = input("请输入你的名:")
print(input_name)

执行结果:

请输入你的名:Earl
Earl

Python2.X代码:

input_name = raw_input("请输入你的名字:")
print input_name

执行结果:

请输入你的名:Earl
Earl

2、在Python3.x与Python2.x中用户交互模块有区别对应关系如下:

Python3.x                              Python2.x

input                                       raw_input

eval(input())                             input()

四、If条件判断与while循环:

(一)、if语句:

1、语法:

if 条件:

print(“要输出信息”)

elif 条件:

print(“要输出信息”)

else:

print(“要输出信息”)

2、例句:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

info = input("请输入您的性别:")
if info == "man":
    print("你是一个帅哥。")
elif info == "girl":
    print("你是一位淑女。")
else:
    print("对不起不知道你的性别。")

 

(二)、while循环:1语法:
while 条件:
print("要输出信息")

 

例句:#!/usr/bin/env python
# -*- coding: utf-8 -*-

while True:
print("info")注:以上语句为死循环语句。

 

(三)、while与if结合使用案例:场景:定义一个数字,然后去猜测这个数字,不论输入数字大于还是小于定义数字均打印相应提示,猜对后退出并只有三次机会:代码如下:#!/usr/bin/env python
# -*- coding: utf-8 -*-
number = 0
lucky_number = 5
while number < 3:
input_number = int(input("请输入0到9之间的任意数字:"))
if input_number > lucky_number:
print("你输入的数字大于幸运数字.")
elif input_number == lucky_number:
print("你输入正确幸运数字.")
break
else:
print("你输入的数字小于幸运数字.")
number += 1
else:
print("对不起,你的机会用完了。")

 

五、数据类型:(一)、数字:1、int(整型)2、long(长整型)3、float(浮点型)(二)、布尔值:1、真或假2、1或0(三)、字符串:“hello world”

万恶的字符串拼接:

python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空间,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
(四)、字符串格式化:
1、普通格式化:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
name = "Earl"
age = 27
info = ("我的名字叫%s, 我的年龄%d.") % (name, age)
print(info)输出:我的名字叫Earl,我的年龄27.注:字符串是%s;整数是%d;浮点数%f2、使用三引号格式化:#!/usr/bin/env python
# -*- coding: utf-8 -*-
name = "Earl"
age = 27
address = "北京"

info = """我的名字:%s
我的年龄:%d
我的住址:%s""" % (name, age, address)
print(info)输出:
我的名字:Earl
我的年龄:27
我的住址:北京3、字符串切片:

2、删除字符串空格:

info.strip() #去除两边的空格

info.lstrip() #去除左边的空格

info.rstrip() #去除右边的空格

六、列表(list):1、创建一个列表:list_info = ["name", "age", "address", 10000]2、列表取值:list_info[0] #0代表列表元素的索引值,值是以0开始的。所以结果为name。list_info[1] #当中括号内是1时取是agelist_info[0:2] #取出列表0到2的元素,但是不包含索引值为2,也就小于2的值。所以结果是name和age。list_info[-1] #取出最后个值,所以结果为10000.list_info[:-1] #取出索引从0开始至倒数第二个字符,不包括-1的值,所以输出结果为name, age, address。3、查看列表方法:>>>dir(list_info)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']注:带有__为私有方法,我们使用不到。

 

4、各种方法演示:append方法是在列表末尾追加元素:list_info = ["name", "age", "address", 10000]
list_info.append("city")print(list_info)
输出结果:['name', 'age', 'address', 10000, 'city']

 

count方法是统计某个元素在列表里的个数:list_info = ["name", "age", "address", 10000, "age"]
info = list_info.count("age")
print(info)输出结果:2

 

insert方法是在列表内插入一个元素:list_info = ["name", "age", "address", 10000]
list_info.insert(2, "job")
print(list_info)输出结果:['name', 'age', 'job', 'address', 10000]注:insert方法中的数字2是索引值,是将新插入的元素放在2这个索引值的位置,而原有的元素全部后移一位。

 

index方法是取出index的索引值:list_info = ["name", "age", "address", 10000]
info = list_info.index("age")
print(info)输出结果:1

 

pop方法是删除列表内的元素:list_info = ["name", "age", "address", 10000]
list_info.pop()
print(list_info)输出结果:['name', 'age', 'address']注:如果pope括号内没有索引值则是删除列表最后一位。如果有索引值则删除索引值的元素。

 

remove方法删除列表元素:list_info = ["name", "age", "address", 10000]
list_info.remove("age")
print(list_info)输出结果:['name', 'address', 10000]

 

sort方法是排序,但是列表内只能是int类型元素:(此方法只在python3.x版本是这样)list_info = [1, 34, 0, 6, 10000]
list_info.sort()
print(list_info)输出结果:[0, 1, 6, 34, 10000]

 
 
 

七、元组(tuple):1、创建元组:tuple_info = ("name", 34, "age", "address", 10000)

 

2、查看元组方法:

>>> dir(tuple_info)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

3、元组使用方法:

元组的操作与列表一样。但是元组只有count与index两种方法。

count方法是统计某个元素在元组里的个数:

tuple_info = ("name", 34, "age", "name", "address", 10000)
info = tuple_info.count("name")
print(info)输出结果:2

index方法是取出index的索引值:

tuple_info = ("name", 34, "age", "address", 10000)
info = tuple_info.index("age")
print(info)输出结果:2

八、列表(list),字符串(str),元组(tuple)说明:

1、相同点:
切片、索引、len() 、in

2、不同点:
str:重新开辟空间
list:修改后,不变

3、元组(tuple):
不能修改

九、运算符

1、算数运算:+ - * / % ** //
2、比较运算:== != <> >= <= > <
3、赋值运算:= += -= *= /= %= **/ //=
4、逻辑运算:and or not
5、成员运算:in not in
6、份运算:is is not
7、运算:& | ^ ~ >> <<

Python学习之路——初识Python的更多相关文章

  1. python学习之路-1 python基础操作

    本篇所涉及的内容 变量 常量 字符编码 用户交互input 格式化字符串 python的缩进规则 注释 初始模块 条件判断 循环 变量 变量的概念基本上和初中代数的方程变量是一致的,只是在计算机程序中 ...

  2. python学习之路-1 python简介及安装方法

    python简介 一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. 目前最新版本为3.5.1,发布于2015年12月07日 ...

  3. python学习之路 初识xml

    import requests from xml.etree import ElementTree as ET r = requests.get('http://www.webxml.com.cn// ...

  4. python学习笔记之初识Python

    一直听说python语音的简单易用而又强大,今天终于忍不住借本书,开始接触接触一下它,下面结合书本和自己的一些体会,写一下刚刚接触python的东西,重点写一些和C++有区别的地方. (1)输入inp ...

  5. Python学习笔记1_初识Python

    一.Python的发展 1.CNRI时期:CNRI是自助Python发展初期的重要单位,Python1.5版之前的成果大部分都在此时期内完成 2.BeOpen时期:Guido van Rossum与B ...

  6. Python学习之路-Day2-Python基础2

    Python学习之路第二天 学习内容: 1.模块初识 2.pyc是什么 3.python数据类型 4.数据运算 5.bytes/str之别 6.列表 7.元组 8.字典 9.字符串常用操作 1.模块初 ...

  7. Python学习之路【第一篇】-Python简介和基础入门

    1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...

  8. python学习之路-day2-pyth基础2

    一.        模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...

  9. Python学习之路-Day2-Python基础3

    Python学习之路第三天 学习内容: 1.文件操作 2.字符转编码操作 3.函数介绍 4.递归 5.函数式编程 1.文件操作 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个 ...

随机推荐

  1. 计算机网络--http代理server的设计与实现

    一.Socket编程的client和服务端的主要步骤: Java Socket编程:对于http传输协议 client: 1.创建新的socket,绑定serverhost和port号 2.Socke ...

  2. BOW

    bag of words(NLP): 最初的Bag of words,也叫做"词袋",在信息检索中,Bag of words model假定对于一个文本,忽略其词序和语法,句法,将 ...

  3. JavaScript引用类型之Array类型一

    一.简介 除了Object之外,Array类型恐怕是ECMAScript中最常用的类型了.下面就来分析ECMAScript中的数组与其他语言中的数组的异同性: 1.相同点: (1)他们都是数据的有序列 ...

  4. SQL学习之空值(Null)检索

    在创建表表,我们可以指定其中的列包不包含值,在一列不包含值时,我们可以称其包含空值null. 确定值是否为null,不能简单的检查是否=null.select语句有一个特殊的where子句,可用来检查 ...

  5. [转]spring 监听器 IntrospectorCleanupListener简介

    "在服务器运行过程中,Spring不停的运行的计划任务和OpenSessionInViewFilter,使得Tomcat反复加载对象而产生框架并用时可能产生的内存泄漏,则使用Introspe ...

  6. 小鱼提问1 类中嵌套public修饰的枚举,外部访问的时候却只能Class.Enum这样访问,这是为何?

    /// <summary> /// 常量等定义 /// </summary> public class General { /// <summary> /// 文件 ...

  7. [C#技术参考]Socket传输结构数据

    最近在做一个机器人项目,要实时的接收机器人传回的坐标信息,并在客户端显示当前的地图和机器人的位置.当然坐标的回传是用的Socket,用的是C++的结构体表示的坐标信息.但是C#不能像C++那样很eas ...

  8. MOSS 2010 无法同步用户配置文件

    The management agent “MOSSAD-Synch AD Connection” failed on run profile “DS_DELTAIMPORT” because of ...

  9. delphi如何获得当前操作系统语言环境

    function GetWindowsLanguage: string; var WinLanguage: ..] of char; begin VerLanguageName(GetSystemDe ...

  10. MFC 动态创建控件

    动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的.   一.创建动态控件:   为了对照,我们先来看一下静态控件的创建.   放置静态控件时必须先建立一个容器,一 ...