[Getting started with Python from scratch] Basic knowledge of Python 3

✍For readers: everyone

✍Column : Python zero-based introductory tutorial

Python 3 is a popular high-level programming language used in a variety of applications. Here are some Python 3 basics you should know:

  1. Variables: In Python 3, variables are created by assigning a value to a name. For example, x = 5 creates a variable named x and assigns it the value 5.
  2. Data types: Python 3 supports a variety of built-in data types, including integers, floats, strings, booleans, lists, tuples, and dictionaries.
  3. Operators: Python 3 supports a variety of operators, including arithmetic operators (+, -, *, /), comparison operators (>, <, ==, !=), and logical operators (and, or, not) .
  4. Control flow statements: Python 3 supports a variety of control flow statements, including if-else statements, for loops, and while loops. These statements allow you to control the flow of execution in your code.
  5. Functions: In Python 3, functions are created using the def keyword. For example, def my_function(x): creates a function named my_function that takes a parameter named x.

Input and Output: In Python 3, you can use the input() function to get user input and the print() function to output text to the console.

Modules: Python 3 supports modules, which are collections of functions and variables that can be imported and used in other Python code. You can import modules using the import keyword.

Advantages of Python 3:

  1. Python 3 has simple syntax and is easy to learn and read, making it a good choice for beginners.
  2. Python 3 is a high-level language with a large standard library and many third-party libraries available, making it a versatile language that can be used in a variety of applications.
  3. Python 3 supports multiple programming paradigms, including object-oriented, functional, and procedural programming.
  4. Python 3 is an interpreted language, which means it does not require compilation before running, making it easy to write and test code quickly.
  5. Python 3 has good support for data analysis and scientific computing, with libraries such as NumPy and Pandas.

Disadvantages of Python 3:

  1. Python 3 can be slower than compiled languages ​​such as C++ or Java, which can be an issue for applications that require high performance.
  2. Python 3 has a Global Interpreter Lock (GIL), which limits its ability to utilize multiple CPU cores.
  3. Python 3 may not be the best choice for low-level systems programming because it does not provide the same level of hardware control as other languages.
  4. Python 3 is not as popular in some areas as other languages, such as R for data analysis or C++ for game development, so it may not always be the best choice for a specific application

Python is an interpreted language, i.e. it is not compiled, the interpreter will check the code line by line. Before we continue... let's go with the most popular "HelloWorld" tradition and compare Python's syntax with C, C++, and Java (I chose these 3 languages ​​because they are the most well-known and commonly used languages) . 

# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.

print("Hello World")	

Note: Please note that Python's scope does not rely on curly braces ( { } ), but uses indentation to indicate its scope. Let's start with Python basics, which we will cover in small sections. Just read them and trust me, you will learn the basics of Python very easily.

Introduction and settings

1. If you downloaded Python on Windows OS by clicking here , now install it from the installer and type IDLE.IDLE in the start menu, you can think of it as a Python IDE for running Python scripts. It will look like this:

2. If you are using an operating system similar to Linux/Unix, just open the terminal. Python is pre-installed on 99% of Linux operating systems. Just type "python3" into the terminal to get started. It looks like this: 

“>>>”代表 python shell 及其准备接受 python 命令和代码。

Variables and data structures

In other programming languages, such as C, C++, and Java, you need to declare the type of a variable, but in Python you don't need to do this. Just enter the variable and when you assign it a value, it will automatically know whether the given value is an int, float, char or String.

# Python program to declare variables
myNumber = 3
print(myNumber)

myNumber2 = 4.5
print(myNumber2)

myNumber ="helloworld"
print(myNumber)

 output

3
4.5
helloworld

See how easy it is, just create a variable and assign it any value you want and print it using the print function. Python has 4 built-in data structures, namely List, Dictionary, Tuple and Set.

Lists are the most basic data structure in Python. A list is a mutable data structure, i.e. items can be added to the list later after the list is created. It's like if you were to shop at a local market and make a list of a few items, then you could add more and more items to the list.
The append() function is used to add data to a list.

# Python program to illustrate a list

# creates a empty list
nums = []

# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")

print(nums)

 output

[21, 40.5, 'string']
# Python program to illustrate a Dictionary

# creates a empty list
Dict = []

# putting integer values
Dict = {1: 'python', 2: 'For', 3: 'python'}

print(Dict)

#Code submitted by Susobhan AKhuli

output 

{1: 'python', 2: 'for', 3: 'python'}

 

# Python program to illustrate a tuple
	
# creates a tuple which is immutable
tup = ('python', 'for', 'python')

print(tup)

#Code submitted by Susobhan AKhuli
(“python”、“for”、“python”)

 

# Python program to illustrate a set

# define a set and its elements
myset = set(["python", "for", "python"])

#as set doesn't have duplicate elements so, 1 geeks will not be printed
print(myset)

#Code submitted by Susobhan Akhuli

output

{'python','for'}

 Comment:

# 在Python中用于单行注释
"""这是一条注释"""用于多行注释

input Output

In this section, we will learn how to get the user's input to act on it or simply display it. The input() function is used to obtain user input

# Python program to illustrate
# getting input from user
name = input("Enter your name: ") 
   
# user entered the name 'harssh'
print("hello", name)

Output:

hello harssh
# Python3 program to get input from user
   
# accepting integer from the user
# the return type of input() function is string ,
# so we need to convert the input to integer
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
   
num3 = num1 * num2
print("Product is: ", num3)

Output:

Enter num1: 8 Enter num2: 6 ('Product is: ', 48)

judgment

Selection in Python is made using the two keywords "if" and "elif" (elseif) and else

# Python program to illustrate
# selection statement
   
num1 = 34
if(num1>12):
   print("Num1 is good")
elif(num1>35):
   print("Num2 is not gooooo....")
else:
   print("Num2 is great")
output
Num1 is great

Guess you like

Origin blog.csdn.net/arthas777/article/details/133465065