零基础学Phyton

Python基础Day1

2019-01-03  本文已影响10人  墨马

注释

作为一个程序员一定要养成写注释的好习惯
单行注释 #
多行注释 """ """

输入

print("Hello word")
print("Hello","word")
//Hello word
输出源码
print(self, *args, sep=' ', end='\n', file=None): # known special case of print
    """
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    """

输入

name = input("请输入姓名:")
print("姓名:",name)
//输入结果为字符串形式

数据类型

占位符%x 16进制数据

变量

import keyword
kw = keyword.kwlist
print(kw)

运行结果

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

强制类型转化 例:num = int("3") num值为数字3
eval:获取字符串中原始数据

my_str = '[1, 3, 5]'
value = eval(my_str)
print(value)
print(type(value))
print(type(my_str))

结果

[1, 3, 5]
<class 'list'>
<class 'str'>

判断语句

if age >= 15 and age <= 17:
    print("少年")
elif age > 17 and age <= 40:
    print("中年")
elif age > 40 and age < 80:
    print("老年")
else:
    print("未知")

比较运算符

逻辑运算符

上一篇 下一篇

猜你喜欢

热点阅读