Python之路——面对Python的第一步

作为当下最火的语言,Python的迷人之处很多,我会记录我Python的学习之路,一步一个脚印,由于我是跟着书上的教程来学习,所以很基础,欢迎大佬指点。

这里我用的版本是python3.6.5,python2和python3还是有很多的不一样,比如说print的使用:
Python2在使用print时可以不加()
Python3在使用print时必须加()(作为函数使用)
再比如说整数除法的时候:
Python2整数除法只保留整数位,和java一样
Python3整数除法可以出来小数

print(3/2)#python2会输出1,而python3会输出1.5

还是从hello_world开始,Python编写就要比Java简要的多:

print("hello world")

一行代码,解决问题。

Python文件是没办法用记事本打开的,它的命名是文件名.py,我用的编辑器是Windows下的notepad++。

如何在命令提示符里运行Python程序呢?

python hello_world.py

hello_world.py这个文件里放的就是上面那行代码,无需编译。

关于变量的命名、文件的命名其实和其它编程语言都差不多:
1.变量名只能包含字母数字下划线,不能以数字开头。
2.变量名不能包含空格。
3.不要将Python关键字和函数名用做变量名。
4.命名要有意义。
简单说一下。

然后说一说字符串:
python中的字符串用单引号或者双引号都可以,而且输出时也很方便,比如说:

print("this is my 'python' blog")

就会输出this is my ‘python’ blog,很方便,单双引号注意点分开用。

此外python中字符串有一些方法比较常用的:
title():首字母大写
upper():全部大写
lower():全部小写
strip():去除字符串两端的空白
lstrip():去除字符串开头的空白
rstrip():去除字符串末尾的空白

print("eric king".title())#输出Eric King
print("asd".upper())#输出ASD
print("ABCD".lower())#输出abcd
print(" aaa ".strip())#输出aaa
print(" aaa ".lstrip())#输出aaa (括号前面有个空格)
print(" aaa ".rstrip())#输出 aaa

通过上面的代码大家也看到了,python中注释是用 #

字符串的拼接呢,和java是一样的,用 **+**号:

print("hello"+" world")

以及制表符 \t、换行符 \n等转义字符,这个我要隆重说一下:

print("hello\tworld")#输出hello		world

这样是没问题的,千万不要这样子写:

print("hello"\t"world")
#或者这样
print("hello"+\t+"world")

不然会报这么一串异常出来:unexpected character after line continuation character
别问我怎么知道的。

然后关于变量的使用和数字类型,python中也很简单:

a=1
b=2
print(a+b)#输出3

在python终端窗口运行的话,你甚至可以直接输入1+2,它会算出来结果是3

>>>1+2#回车
3

让我一度把python当计算器用…

最后,请你在python终端窗口输入import this,看一看python之禅:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!
欢迎英文大佬翻译。

至于命令行如何打开python终端回话窗口,只需要输入python,当然你要提前配置好环境变量,它会输出python的版本信息;而退出呢只需要Ctrl+z之后回车就好了。

这是我python的第一课,欢迎大家指正。
谢谢您的观看。

猜你喜欢

转载自blog.csdn.net/weixin_43622082/article/details/86485627