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)
2. Check Add python to PATH to add The path needs attention and must be checked!
3. Do not change the default and proceed to Next step
4.Choose an installation location you like
Click Install to start the installation
to enter your own installation directory and run the statement: python -V
If the corresponding version of Python is displayed, the installation is successfulPython 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>
(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") # 连接字符串
(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) # 连接列表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)
实例:
#!/usr/bin/python3 student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'} print(student) # 输出集合,重复的元素被自动去掉
(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()) # 输出所有值
以上实例输出结果:
1 - Python教程 2 - Python工具 {'name': 'demo', 'site': 'www.demo.com', 'code': 1} dict_keys(['name', 'site', 'code']) dict_values(['demo', 'www.demo.com', 1])
另外,字典类型也有一些内置的函数,例如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) # 连接元组
开始接触这些有点记不住,但是要加油鸭。相信多练习一定会记住的
3.分支/选择结构
分支结构一共分为4类:单项分支,双项分支,多项分支,巢状分支
(1)单项分支
if 条件表达式: 一条python语句... 一条python语句... ...
特征:
if条件表达式结果为真,则执行if之后所控制代码组,如果为假,则不执行后面的代码组(:后面的N行中有相同缩进的代码)
:之后下一行的内容必须缩进,否则语法错误!
if之后的代码中如果缩进不一致,则不会if条件表达式是的控制,也不是单项分支的内容,是顺序结构的一部分
if:后面的代码是在条件表达式结果为真的情况下执行,所以称之为真区间或者if区间、
(2) 双项分支
if 条件表达式: 一条python语句... 一条python语句... ... else: 一条python语句... 一条python语句... ...
特征:
1.双项分支有2个区间:分别是True控制的if区间和False控制的else区间(假区间)
2.if区间的内容在双项分支中必须都缩进,否则语法错误!
(3) 多项分支
if 条件表达式: 一条python语句... 一条python语句... ... elif 条件表达式: 一条python语句... 一条python语句... ... elif 条件表达式: 一条python语句... 一条python语句... ... ... else: 一条python语句... 一条python语句...
特征:
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)
(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")
将以上程序保存到 test_if.py 文件中,执行后输出结果为:
$ python3 test.py 输入一个数字:6 你输入的数字可以整除 2 和 3
感谢大家的阅读,希望大家收益多多。
推荐教程:《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!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software