python first lesson - the basics

About python

Python is a computer programming language. Is a dynamically typed object-oriented language, originally designed for writing automated scripts (shell), constantly updated with the addition of new features and language versions, is used more and more independent, large-scale development projects.

Python Applications

Python is an interpreted scripting language that can be used in the following areas: 
  • Web and Internet Development
  • Scientific Computing and Statistics
  • artificial intelligence
  • education
  • Desktop interface development
  • Software Development
  • Back-end development

Download Python

Download python URL: https://www.python.org/        # compiler

Download anaconda website: https://www.anaconda.com/    # environment

Two to download and install software

Write the first code

print("Hello Word")

Note

  •     #: You can comment line
  • "" "" "": You can comment multiple lines, if a variable is assigned to a string inside the content
# What a beautiful day 
. "" " Today's raining ." "" 
A = "" " the sound of thunder big " "" 
Print (A)

 

Variables and types

  • Integer: Python can handle any size integer (Python 2.x, there are two types int and long integer, but this distinction is of little significance for Python, only integer int in Python 3.x in this one kind of), but also support binary (eg 0b100, converted to decimal is 4), octal (eg 0o100, converted to decimal is 64), decimal ( 100) and hexadecimal ( 0x100converted to decimal 256) representation.

  • Floating-point type: decimal floating point is, is called float, because when expressed in terms of scientific notation, decimal point position a floating-point number is variable, floating point math in addition to the wording (such as 123.456addition) also supports scientific notation (such as 1.23456e2).

  • String: string is a single or double quotes any text, such as 'hello'and "hello", as well as the string representation of the original string, byte string representation, Unicode string representation, and can be written in a plurality of in rows (beginning with three single quotes, or three double quotes, three single or double quotation marks the end of three).

  • Boolean: Boolean only True, Falsetwo kinds of values, either True, or is False, in Python, can be directly used True, Falsea Boolean value indicating (note the case), can also be calculated by Boolean operators (e.g., 3 < 5generates a Boolean value True, and 2 == 1It will produce a Boolean value False).

  • Complex type: the form 3+5j, represents a complex mathematical with the same, the only difference is the imaginary part ireplaced j.

Variable naming

For each variable we need to give it a name, just as each of us has his own famous name the same. In Python, variable naming need to follow these rules must comply with rigid and non-rigid rules is strongly recommended to follow.

  • Hard and fast rule:

    • 变量名由字母(广义的Unicode字符,不包括特殊字符)、数字和下划线构成,数字不能开头。

    • 大小写敏感(大写的a和小写的A是两个不同的变量)。

    • 不要跟关键字(有特殊含义的单词,后面会讲到)和系统保留字(如函数、模块等的名字)冲突。

  • PEP 8要求:

    • 用小写字母拼写,多个单词用下划线连接。

    • 受保护的实例属性用单个下划线开头(后面会讲到)。

    • 私有的实例属性用两个下划线开头(后面会讲到)。

当然,作为一个专业的程序员,给变量(事实上应该是所有的标识符)命名时做到见名知意是非常重要的。

变量的使用

  • 函数输入
a = input()  #接受的是字符串
print(a,type(a)) #type()检查变量的类型

  • 加法计算
ChangeToFloat_1=float(input("请输入数字:"))
ChangeToFloat_2=float(input("请输入数字:"))
print("结果:",ChangeToFloat_1+ChangeToFloat_2)

  • sep:更改多个元素之间的连接符
  • print("hello","word")
    print("hello","word",sep=",")

  • end:更改结尾符,默认为换行符
  • print("hello","word")
    print("hello","word",end="!")
    print("ok")

 

  • 死循环
while 1:
    print("hello word")

  • 运算
//  :整除     例子:5 // 2 = 2
**  :幂指数   例子:5 ** 2 = 25

 强制转换

  • int():将一个数值或字符串转换成整数,可以指定进制。

  • float():将一个字符串转换成浮点数。

  • str():将指定的对象转换成字符串形式,可以指定编码。 默认为空字符串

  • chr():将整数转换成该编码对应的字符串(一个字符)。 例如:chr(97) = a 

  • ord():将字符串(一个字符)转换成对应的编码(整数)。例如: ord('b') = 98

 运算符

运算符 描述
[]   [:] 下标,切片
** 指数
~   +   - 按位取反, 正负号
/  %  // 乘,除,模,整除
+   - 加,减
>>  << 右移,左移
& 按位与
| 按位异或,按位或
<=   <   >   >= 小于等于,小于,大于,大于等于
==  != 等于,不等于
is      is not 身份运算符
in      not in 成员运算符
not      or    and 逻辑运算符
+=  -=  *=  /=  %=  //=  **=  &=  |=  ^=  >>=  <<= (复合)赋值运算符
  • 左移/右移 : 3<<2    首先把3转为二进制:00000011,之后把最高位左移两位,结果为001100
  • == 判断数值大小    is 判断空间地址     数值在2000左右公用一个地址空间

 格式化输出

print("{}真帅".format("这个男孩"))

b=12.333333
print("{:.2f}".format(b))

a =100
print("%d"%a)

input_ = input()
"{}好美".format(input_)

for i in "abcd":
    print(i) 

for i in range(5):
    print("02")

加密

char_=input("请输入Email:")
for i in char_:
    ord_=ord(i)+4
    chr_=chr(ord_)
    print(chr_,end="")

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/gxnihao/p/11269434.html