The first lesson of Python basics

Source: Internet
Author: User

GitHub Blog Portal
CSDN Blog Portal

Start our Python learning path ready to install Pycharm

GitHub article links Click I see how to install Python
CSDN article link Click I see how to install Python

Create a new project open Pycharm click

Select your project path and project name

Create a new Python file

Enter the name of the new Python file click OK

We're still hello world, according to the programmer's learning routine.

Input:

print ‘Hello Wrold‘

It's not up to the map.

Try entering and observing:

print ‘hello world‘print "hello world"print 3print 3+2print ‘3‘print ‘3+2‘print 3,3+2,‘3‘,‘3+2‘

Watch out for the difference between print 3+2 and print ' 3+2 '

Output Result:

hello worldhello world3533+23 5 3 3+2
The first way to annotate a comment

A single-line comment in Python starts with #.

#!/usr/bin/python# -*- coding: UTF-8 -*-# 文件名:test.py# 第一个注释print "Hello, World!";  # 第二个注释

Output Result:

Hello, World!

Annotations can be at the end of a statement or an expression line:

name = "Mrzhang" # 这是一个注释
The second method of commenting

A multiline comment in Python uses three single quotation marks ("') or three double quotation marks (" ").

# 单引号或双引号  ‘‘‘此处被三个单引号包裹的内容不会被执行 此处被三个单引号包裹的内容不会被执行此处被三个单引号包裹的内容不会被执行‘‘‘  """此处被三个双引号包裹的内容不会被执行此处被三个双引号包裹的内容不会被执行此处被三个双引号包裹的内容不会被执行"""  
Python Common data type data type int

Integers contain positive and negative integers, represented by Int.
Cases:

1234, -666, 0, 99999999999
Data type float

float float represents decimals, with a decimal point, or a scientific count sign E or E
Cases:

1.23, 1., 3.14e-10, 4E210, 4.0e+210
Data type bool

There is no Boolean in Python2, which represents false with the number 0 and 1 for true.
True and false are defined as keywords in Python3, but their values are still 1 and 0, and they can be added to the numbers. However, we do not generally do this because the bool type is used to judge.

Data type complex (plural)

The plural constants of Python are written in real + imaginary parts, and imaginary parts end with J or J.
Cases:

 3+4j, 3.0+4.0j, 3J
View data types

To view the data type of a data using the built-in function of type
Self-Test the following example

print type(1)print type(‘abc‘)print type(True)print type(3+4j)

Output Result:

<type ‘int‘><type ‘str‘><type ‘bool‘><type ‘complex‘>
The meaning of variable variables

A variable is a piece of the computer that creates a!!! Memory space that is not being used!!! To store all the data I want to store, this space, or container, is a variable.
Constants: Volume (data) that cannot be changed

How to define Variables

Naming rules

Variable names consist of letters, numbers, underscores, cannot start with a number, and are sensitive to letter capitalization. Can't use keywords (python's official reserved Keywords)

Defining formats

Variable name = initialization value

Cases:
#!/usr/bin/python# -*- coding: UTF-8 -*-num1 = 3    # 相当于容器名为num1存储了3这个值print num1    # 打印这个变量的值,这个变量存储的3也就是输出3# 连续输入num2 = 4  num2 = 8num2 = 4num2 = 8print num2    # 将会输出 8 相当于先给容器num2放了 4 这个值然后再给容器num2放了 8 这个值 则会输出 8 因为一个容器只能存储一个常量a = ‘你好‘     # 相当于容器名为 a 存储了  你好  这个值print a       # 打印出 a 容器存储的值

Output Result:

38你好
Formatting the usage of format
#!/usr/bin/python# -*- coding: UTF-8 -*-a = 5b = 3# 使用占位符print "5+3={}".format((a+b))# 更改一下大括号内的数值查看结果print "5+3={0}".format((a+b),5,6,7)print "5+3={1}".format((a+b),5,6,7)print "5+3={2}".format((a+b),5,6,7)print "5+3={3}".format((a+b),5,6,7)

Output Result:

5+3=85+3=85+3=55+3=65+3=7
Manipulating indexes/slices of strings
#!/usr/bin/python# -*- coding: UTF-8 -*-# 查看字符串的长度str1 = "23123iuafdhdhfjkh4h343434afdffdd13"print len(str1)          # 可以直接打印出 str 字符的长度# 索引操作str = "0123456789"print str[4]             # 输出4 索引为第五个的数据print str[0]             # 输出0 索引为第一个的数据print str[-1]            # 输出9 索引为倒数第一个的数据# 切片操作print str[0:3]           # 输出012 索引为第一个到第三个但不包括第三个的数据print str[4:len(str)]    # 输出456789 索引为第五个到字符串长度的值print str[:]             # 输出0123456789 索引为整个字符串print str[5:-2]          # 输出567 索引为第六个到倒数第二个不包括倒数第二个

Output Result:

34           # print len(str1)4            # print str[4]0            # print str[0]9            # print str[-1]012          # print str[0:3]456789       # print str[4:len(str)]0123456789   # print str[:]567          # print str[5:-2]
Merging of strings
#!/usr/bin/python# -*- coding: UTF-8 -*-str1 = ‘123‘str2 = ‘456‘print str1 + str2    # 输出123456 将两个字符串相加print str1 * 3       # 输出123123123 将str1字符串输出三遍

Output Result:

123456123123123
Line and indent

The biggest difference between learning Python and other languages is that Python's code block does not use curly braces {} to control classes, functions, and other logical judgments. Python's most distinctive feature is the use of indentation to write modules.

The amount of whitespace indented is variable, but all code block statements must contain the same amount of indentation whitespace, which must be strictly enforced. As shown below:

if True:    print "True"else:    print "False"
Binary conversion

Here's how to convert a decimal 18 to a binary octal hexadecimal

The program is expressed as:

bin(18)    # 将 十进制18 转换为 二进制 的写法oct(18)    # 将 十进制18 转换为 八进制 的写法hex(18)    # 将 十进制18 转换为 十六进制 的写法int("0b10010",2)    # 将 二进制18 转换为 十进制 的写法int("022",8)        # 将 八进制18 转换为 十进制 的写法int("0x12",16)      # 将 十六进制18 转换为 十进制 的写法bin(18)    # 将 十进制18 转换为 二进制 的写法oct(18)    # 将 十进制18 转换为 八进制 的写法hex(18)    # 将 十进制18 转换为 十六进制 的写法int("0b10010",2)    # 将 二进制18 转换为 十进制 的写法int("022",8)        # 将 八进制18 转换为 十进制 的写法int("0x12",16)      # 将 十六进制18 转换为 十进制 的写法

The first lesson of Python basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.