Home > Backend Development > Python Tutorial > Learning experience for beginners in python

Learning experience for beginners in python

烟雨青岚
Release: 2020-06-17 13:01:56
forward
3953 people have browsed it

Learning experience for beginners in python

Beginner’s learning experience of python

Introduction to Python

As a highly praised language in recent years, Python has some advantages that cannot be ignored. Python is a high-level scripting language that combines interpreted, compiled, interactive and object-oriented scripting. It is designed to be highly readable, often uses English keywords, some punctuation marks from other languages, and it has a more distinctive grammatical structure than other languages. Python is an interpreted language: this means there is no compilation part of the development process. Similar to PHP and Perl languages. Python is also an interactive language: this means that you can write programs directly and interactively from a Python prompt. It is an object-oriented language: This means that Python supports object-oriented style or programming techniques in which code is encapsulated in objects. There were so many advantages that I finally chose it.

Because I just learned it, I need to install the python environment first.

1. Python environment setup (windows environment)

1. Download address: https://www.python.org/downloads/windows/

Choose the computer that suits you Number of bits to download the installation package (ps: x86 represents a 32-bit system, 64 represents a 64-bit system)

Learning experience for beginners in python

2. Check Add python to PATH to add The path needs attention and must be checked!

Learning experience for beginners in python

3. Do not change the default and proceed to Next step

Learning experience for beginners in python

4.Choose an installation location you like

Click Install to start the installation

Learning experience for beginners in python

##5. After the installation is completed, click Close to close

If the word Administrator appears in the box, click Authorize and then close

Learning experience for beginners in python

6. Verification: Run cmd

to enter your own installation directory and run the statement: python -V

If the corresponding version of Python is displayed, the installation is successful

Learning experience for beginners in python

Python download and installation address:

https://t.csdnimg.cn/h5DQ

2. Python basic data types

After the environment is successfully established, python learning begins. First, learn the basic data types of python: there are seven types

(1) Number (number)

Python3 supports int, float, bool, and complex (plural).

In Python 3, there is only one integer type int, which is expressed as a long integer. There is no Long in python2.

Like most languages, assignment and calculation of numeric types are very intuitive.

The built-in type() function can be used to query the type of object pointed to by the variable.

>>> a, b, c, d = 20, 5.5, True, 4+3j
>>> print(type(a), type(b), type(c), type(d))
<class> <class> <class> <class></class></class></class></class>
Copy after login

(2) String

Strings in Python are enclosed in single quotes (') or double quotes ("), and use back Slash () escapes special characters.

The syntax format for string interception is as follows:

Variable [head subscript: tail subscript]

The index value starts with 0 is the starting value, -1 is the starting position from the end.

The plus sign ( ) is the connection symbol of the string, the asterisk (*) means copying the current string, and the number that follows is the number of copies. Examples are as follows:

#!/usr/bin/python3
str = 'zhangsan'
print (str) # 输出字符串
print (str[0:-1]) # 输出第一个到倒数第二个的所有字符
print (str[0]) # 输出字符串第一个字符
print (str[2:5]) # 输出从第三个开始到第五个的字符
print (str[2:]) # 输出从第三个开始的后的所有字符
print (str * 2) # 输出字符串两次
print (str + "TEST") # 连接字符串
Copy after login

(3) List

List is the most frequently used data type in Python.

List The data structure implementation of most collection classes can be completed. The types of elements in the list can be different, it supports numbers, strings can even contain lists (so-called nesting).

Lists are written in square brackets [] A list of elements separated by commas.

Like strings, lists can also be indexed and truncated. After the list is truncated, a new list containing the required elements is returned.

The syntax format of list interception is as follows:

Variable [head subscript: tail subscript]

The index value starts with 0, and -1 is the starting position from the end.

The plus sign ( ) is the list connection operator, and the asterisk (*) is the repeat operation. The following example:

#!/usr/bin/python3
list = [ 'abcd', 786 , 2.23, 'demo', 70.2 ]
tinylist = [123, 'demo']
print (list) # 输出完整列表
print (list[0]) # 输出列表第一个元素
print (list[1:3]) # 从第二个开始输出到第三个元素
print (list[2:]) # 输出从第三个元素开始的所有元素
print (tinylist * 2) # 输出两次列表
print (list + tinylist) # 连接列表
Copy after login
List has many built-in methods, such as append(), pop(), etc.

*Notice:

1、List写在方括号之间,元素用逗号隔开。2、和字符串一样,list可以被索引和切片。3、List可以使用+操作符进行拼接。4、List中的元素是可以改变的。

(4)Set(集合)

集合(set)是一个无序不重复元素的序列。

基本功能是进行成员关系测试和删除重复元素。

可以使用大括号 { } 或者set()函数创建集合,注意:创建一个空集合必须用set()而不是 { },因为 { } 是用来创建一个空字典。

创建格式:

parame = {value01,value02,...}
或者
set(value)
Copy after login

实例:

#!/usr/bin/python3
student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}
print(student) # 输出集合,重复的元素被自动去掉
Copy after login

(5)Dictionary(字典)

字典(dictionary)是Python中另一个非常有用的内置数据类型。

列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

字典是一种映射类型,字典用"{ }"标识,它是一个无序的键(key) : 值(value)对集合。

键(key)必须使用不可变类型。

在同一个字典中,键(key)必须是唯一的。

#!/usr/bin/python3
dict = {}
dict['one'] = "1 - Python教程"
dict[2] = "2 - Python工具"
tinydict = {'name': 'demo','code':1, 'site': 'www.demo.com'}
print (dict['one']) # 输出键为 'one' 的值
print (dict[2]) # 输出键为 2 的值
print (tinydict) # 输出完整的字典
print (tinydict.keys()) # 输出所有键
print (tinydict.values()) # 输出所有值
Copy after login

以上实例输出结果:

1 - Python教程
2 - Python工具
{'name': 'demo', 'site': 'www.demo.com', 'code': 1}
dict_keys(['name', 'site', 'code'])
dict_values(['demo', 'www.demo.com', 1])
Copy after login

另外,字典类型也有一些内置的函数,例如clear()、keys()、values()等。

注意:

1、字典是一种映射类型,它的元素是键值对。

2、字典的关键字必须为不可变类型,且不能重复。

3、创建空字典使用 { }。

(6)Tuple(元组)

元组(tuple)与列表类似,不同之处在于元组的元素不能修改。元组写在小括号(())里,元素之间用逗号隔开。

元组中的元素类型也可以不相同:

#!/usr/bin/python3
tuple = ( 'abcd', 786 , 2.23, 'demo', 70.2 )
tinytuple = (123, 'demo')
print (tuple) # 输出完整元组
print (tuple[0]) # 输出元组的第一个元素
print (tuple[1:3]) # 输出从第二个元素开始到第三个元素
print (tuple[2:]) # 输出从第三个元素开始的所有元素
print (tinytuple * 2) # 输出两次元组
print (tuple + tinytuple) # 连接元组
Copy after login

开始接触这些有点记不住,但是要加油鸭。相信多练习一定会记住的

3.分支/选择结构

分支结构一共分为4类:单项分支,双项分支,多项分支,巢状分支

(1)单项分支

if 条件表达式:
 一条python语句...
 一条python语句...
 ...
Copy after login

特征:

if条件表达式结果为真,则执行if之后所控制代码组,如果为假,则不执行后面的代码组(:后面的N行中有相同缩进的代码)

:之后下一行的内容必须缩进,否则语法错误!

if之后的代码中如果缩进不一致,则不会if条件表达式是的控制,也不是单项分支的内容,是顺序结构的一部分

if:后面的代码是在条件表达式结果为真的情况下执行,所以称之为真区间或者if区间、

(2) 双项分支

if 条件表达式:
 一条python语句...
 一条python语句...
 ...
else:
 一条python语句...
 一条python语句...
 ...
Copy after login

特征:

1.双项分支有2个区间:分别是True控制的if区间和False控制的else区间(假区间)

2.if区间的内容在双项分支中必须都缩进,否则语法错误!

(3) 多项分支

if 条件表达式:
 一条python语句...
 一条python语句...
 ...
elif 条件表达式:
 一条python语句...
 一条python语句...
 ...
elif 条件表达式:
 一条python语句...
 一条python语句...
 ...
...
else:
 一条python语句...
 一条python语句...
Copy after login

特征:

1.多项分支可以添加无限个elif分支,无论如何只会执行一个分支

2.执行完一个分支后,分支结构就会结束,后面的分支都不会判断也不会执行

3.多项分支的判断顺序是自上而下逐个分支进行判断

4.在Python中没有switch – case语句。

实例-演示了狗的年龄计算判断:

#!/usr/bin/python3
age = int(input("请输入你家狗狗的年龄: "))
print("")
if age  2:
 human = 22 + (age -2)*5
 print("对应人类年龄: ", human)
Copy after login

(4) 巢状分支

巢状分支是其他分支结构的嵌套结构,无论哪个分支都可以嵌套

# !/usr/bin/python3
num=int(input("输入一个数字:"))
if num%2==0:
 if num%3==0:
 print ("你输入的数字可以整除 2 和 3")
 else:
 print ("你输入的数字可以整除 2,但不能整除 3")
else:
 if num%3==0:
 print ("你输入的数字可以整除 3,但不能整除 2")
 else:
 print ("你输入的数字不能整除 2 和 3")
Copy after login

将以上程序保存到 test_if.py 文件中,执行后输出结果为:

$ python3 test.py 
输入一个数字:6
你输入的数字可以整除 2 和 3
Copy after login

感谢大家的阅读,希望大家收益多多。

推荐教程:《python教程

The above is the detailed content of Learning experience for beginners in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template