Python-02- basics

A first Python program

 

[] The first step to create a new hello.txt

 

 

[] The second step will be changed to txt extension py

 

[The third step] using Notepad to edit the file

 

[Fourth Step] run the file in cmd

 

print("Hello World!")

Stressed: python interpreter execution program is interpreted, that is, open the file read content, the file extension is no hard limit, but is usually defined as .py ending
the previous code must be added in the Linux system   # / usr / bin / env! python

Second, use the interactive mode to run the program

Third, comments

1. The role comment

Note substantially effect can be summarized in the following two:

  • By using their own familiar language, in the program code to mark instructions can greatly enhance readability
  • The development process, when some code to remove most of the time we will choose to remove comments form

2. Classification comments

  • Single-line comments

    Start with #, everything # as the right explanation, rather than real programs to be executed, play a supporting role in explanation

# The following program, printouts world PyCharm the Hello 

Print ( " the Hello world PyCharm " )
  • Multi-line comments

    Multi-line comments in python with three pairs of marks is completed, it may be a single quotes may be a double quotes

'''

print("hello world pycharm")

print("hello world pycharm")

print("hello world pycharm")

print("hello world pycharm")

print("hello world pycharm")

print("hello world pycharm")

'''

四、变量

1. 什么是变量

  变量即变化的量,核心是“变”与“量”二字,变即变化,量即衡量状态

  程序执行的本质就是一系列状态的变化,变是程序执行的直接体现,所以我们需要有一种机制能够反映或者说是保存下来程序执行时状态以及状态的变化。

2. 变量定义的规则

  • 变量名只能是 字母、数字或下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 以下关键字不能声明为变量名
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

3.变量的定义方式

  • 驼峰体

    AgeOfOldboy = 56

    NumberOfStudents = 80

  • 下划线(推荐使用)

    age_of_oldboy = 56

    number_of_students = 80

     定义变量名不好的方式:

    1. 变量名为中文、拼音

    2. 变量名过长

    3. 变量名词不达意

五、输入输出

1. 输入

  • 在python3中

    input:用户输入任何值,都存成字符串类型

  • 在python2中

    input:用户输入什么类型,就存成什么类型

    raw_input:等于python3的input

2. 输出

  程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式

  比如要求用户输入用户名和年龄,然后打印如下格式:

 

  My name is xxx,my age is xxx.

 

  很明显,用逗号进行字符串拼接,只能把用户输入的名字和年龄放到末尾,无法放到指定的xxx位置,而且数字也必须经过str(数字)的转换才能与字符串进行拼接。

 

  这就用到了占位符,如:%s、%d

 

#%s字符串占位符:可以接收字符串,也可接收数字
print('My name is %s,my age is %s' %('name',18)) #%d数字占位符:只能接收数字 print('My name is %s,my age is %d' %('name',18)) print('My name is %s,my age is %d' %('name','18')) #报错 #接收用户输入,打印成指定格式 name=input('your name: ') age=input('your age: ') #用户输入18,会存成字符串18,无法传给%d print('My name is %s,my age is %s' %(name,age)) #注意: #print('My name is %s,my age is %d' %(name,age)) #age为字符串类型,无法传给%d,所以会报错

 

常用的占位符:

格式符号

转换对应的类型

%c

字符

%s

通过str() 字符串转换来格式化

%i

有符号十进制整数

%d

有符号十进制整数

%u

无符号十进制整数

%o

八进制整数

%x

十六进制整数(小写字母)

%X

十六进制整数(大写字母)

%e

索引符号(小写'e')

%E

索引符号(大写“E”)

%f

浮点实数

%g

%f和%e 的简写

%G

%f和%E的简写

六、数据类型

 

 

 

Guess you like

Origin www.cnblogs.com/lsf123456/p/11129421.html