Python开发基础: 基本语法

Python环境部署(Windows and Linux)

##在Windows中部署Python 3.7版本
    1. 访问https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64.exe下载包.
    2. 双击安装即可,注意勾选“Add Python 3.7 to PATH”添加环境变量,其余步骤均为下一步,建议直接安装在C盘.
​
##在Linux中部署Python 3.7版本
[baidu@cloud_python ~]$ sudo yum -y groupinstall "Development Tools"
[baidu@cloud_python ~]$ sudo yum install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel libffi-devel
[baidu@cloud_python ~]$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
[baidu@cloud_python ~]$ sudo tar xf Python-3.7.2.tgz -C /opt/
[baidu@cloud_python ~]$ cd /opt/Python-3.7.2/
[baidu@cloud_python Python-3.7.2]$ sudo vim Modules/Setup.dist
readline readline.c -lreadline -ltermcap
SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto
​
[baidu@cloud_python Python-3.7.2]$ sudo ./configure --enable-shared     --预编译
[baidu@cloud_python Python-3.7.2]$ sudo make -j 2                                           --编译,j=CPU核数
[baidu@cloud_python Python-3.7.2]$ sudo make install                                    --安装
​
[baidu@cloud_python ~]$ sudo vim /etc/profile.d/python3_lib.sh              --添加环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
​
[baidu@cloud_python ~]$ sudo vim /etc/ld.so.conf.d/python3.conf             --添加环境库文件
/usr/local/lib
​
[baidu@cloud_python ~]$ source /etc/profile
[baidu@cloud_python ~]$ sudo ldconfig
PyCharm的安装和使用

下载地址: Download PyCharm: Python IDE for Professional Developers by JetBrains

安装心法: Windows下一步, Mac复制黏贴, Linux解压就得

变量(vairable)
# 指在计算机编程中与关联的标识符配对的内存存储位置,在使用时含有相关类型的值,其值可以修改.
# 声明变量的时候,必须给予初始值,否则就会报错,如下所示:
>>> print(a)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = 10
>>> print(a)
10
# 这里我们称a为变量的名字,称=为赋值符号,称10为值
# 变量的赋值方式有很多,我们常用的除了上述的一种之外还有两种常常使用.
>>> a = b = c = 28
>>> print(a, b, c)
28 28 28
​
>>> a, b, c = 26, 27, 28
>>> print(a, b, c)
26 27 28
​
>>> d = input("please input your variable: ")
please input your variable: 120
>>> print(d)
120
# 上述的变量被赋值的类型都是数字, 我们称数字类型的变量为“整型”或“浮点型”.
# 但是python中不仅仅只有这两种类型, 还有“字符串”和“布尔值”类型.
# 它们共同组成了python中的基本数据类型.
基本数据类型
# 字符串型变量
>>> var01 = "hello world"
>>> print(var01, type(var01))
hello world <class 'str'>
​
>>> var02 = 'hello world'
>>> print(var02, type(var02))
hello world <class 'str'>
​
>>> var03 = "this's my girl friend"
>>> print(var03, type(var03))
this's my girl friend <class 'str'>
# 整型变量及浮点型变量
>>> num01 = 28
>>> print(num01, type(num01))
28 <class 'int'>
​
>>> num02 = 3.1415926
>>> print(num02, type(num02))
3.1415926 <class 'float'>
# 布尔值类型
>>> bool01 = True
>>> print(bool01, type(bool01))
True <class 'bool'>
​
>>> bool02 = False
>>> print(bool02, type(bool02))
False <class 'bool'>
各路运算符
# 算数运算符
- “+”、“-”、“*”、“/”、“%”、“//” 分别对应加、减、乘、除、取余(求模)、整除
​
# 比较运算符
- “<”、“>”、“<=”、“>=”、“==”、“!=” 分别对应小于、大于、小于等于、大于等于、等于、不等于 
​
# 赋值运算符
- “=”、“+=”、“-=” 分别对应赋值、自增、自减
​
# 逻辑运算符
- “and”、“or”、“not” 分别对应与、或、非

流程控制(Python basis syntax)
# 条件判断
if boolean:
  subcode block 1
else:
  subcode block 2

# case01: According to the user's input, the corresponding result is obtained.
user_input_01 = int(input("please input your number: "))
if type(user_input_01) == int:
    if user_input_01 > 20:
        print("your number is more than 20.")
    else:
        print("your number is less than 20.")
else:
    print("TypeError: invalid value should be input integer variable.")


# case02: According to the user's input, the corresponding result is obtained, version v2.
user_input_02 = int(input("please input your number: "))
if 20 < user_input_02 and user_input_02 < 30:
    print("The number location 20 of 30 between.")
elif 30 < user_input_02 or user_input_02 < 40:
    print("The number location all.")
else:
    print("The number is: ", user_input_02)

# case01(for): Find all prime numbers between 1 and 100.
for prime in range(2, 101):
    for i in range(2, prime):
        if prime % i == 0:
            break
    else:
        print(prime)

# case02(while): find all prime numbers between 1 and 100.
prime = 2
while prime < 101:
    i = 2
    while i < prime:
        if prime % i == 0:
            break
        i += 1
    else:
        print(prime)
    prime += 1

课后练习题:爱因斯坦阶梯

  • 若每步上2阶,最后剩下1阶;

  • 若每步上3阶,最后剩2阶;

  • 若每步上5阶,最后剩下4阶;

  • 若每步上6阶,最后剩5阶;

  • 只有每步上7阶, 最后刚好一阶也不剩

请问这个长阶梯有多少阶?(请在评论区内回答或者私信)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学技术oH

你的鼓励是一起学习的动力何阶梯

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值