[Python learning notes] how to learn python with C# Foundation

Recommended reading

1, Foreword

I already have a certain C# foundation, so it's easy to learn python, but it's also easy to get confused. So I want to compare and learn some Python syntax with C# and analyze and summarize it.

2, Text

2-1 notes

Comments for Python

Single line comment (#)

# I'm a line of notes
print("hello world")  # I'm also a line of notes

Multiline comment ("" ")

multiline comment 
All three quotes are comments
print("hello world")

C #'s note

Single line comment (/ /)

// I'm a line of comments. Note the semicolon in the back. python syntax doesn't have a semicolon in this line
print("hello world");

Multiline comment (use / / or / * * /)

Long note
 It's full of notes
print("hello world");

///Multiline comment
///It is mostly used to describe the precautions and functions of class method variables
print("hello world");

2-2 variables

python variable naming

name = "Zhang San"
age = 18
adult = False

You can see that the variable naming of python does not need to set the variable type, but the conversion of variables should be in the correct format
For example, string type cannot be added to int type

C #'s variable naming

string name = "Zhang San";
int age = 18;
bool adult = false;

2-3 mathematical operation

Arithmetic operators in python:

# addition
a = 1+1
# subtraction
b = 1-1
# multiplication
c = 2*5
# division
d = 4/2
# Remainder extraction
e = 5/2
# Power operation
f = 2**3

python assignment operator:

# The addition and assignment operator assigns the result of the right operand plus the left operand to the left operand
a += 1
# The minus and assignment operator assigns the result of subtracting the right operand from the left operand to the left operand
a -= 1
# The multiply and assign operator assigns the result of multiplying the right operand by the left operand to the left operand
a *= 1
# The division and assignment operator assigns the result of dividing the left operand by the right operand to the left operand
a /= 1

Arithmetic and assignment operators of C #

c = a + b;
c = a - b;
c = a * b;
c = a / b;
c = a % b;
//Additive assignment operator
c += a;
//Minus and assignment operator
c -= a;
//Multiply and assign operator
c *= a;
//Division and assignment operator
c /= a;

2-4 process control

python's if statement uses

if statement

age = 20
if (age >= 18):

if... else statement

age = int(input("Please enter your age:"))
if age >= 18:
    print("under age")

if…elif…elif.else statement

money = int(input("Please enter your income:"))
if money <= 800:
    print("No tax")
elif money > 800 and money <=4000:
    print("The amount of tax paid is:",(money-800)*0.2)
elif money>4000 and money<20000:
    print("The amount of tax paid is:", money * 0.16)
    print("You earn too much. Deduct it all")

C #'s if statement uses

if statement

int age = 20;
if (age >= 18)

if... else statement

int age = 20;
if (age >= 18)
     print("under age");

if…else if …else if.else statement

int age = 20;
if (age <= 1)
else if(age >1 && age <3)
else if (age > 3 && age < 18)
    print("under age");

2-5 list (array)

Python list

List, English name list, is a data type in Python that can dynamically add and delete content, which is composed of a series of elements. To put it bluntly, a list is a container that combines multiple variables.
Lists in Python are similar to the array concept in other languages

List initialization:

my_list = ["apple", "orange", "grape", "pear"]

List read:

my_list = ["apple", "orange", "grape", "pear"]
print("The elements with index 0 are:", my_list[0])

List slice:

# Read list elements from index m to n-1
# Read the first n elements of the list
# Reads the elements of the list from m to the end
# Interval s, reading list elements from m to n

List common functions:

my_list = [1,2,3,4,5,6]
# Find the maximum value

my_list1 = [1,2,3,4,5,6]
# Sum

List modification and deletion:

my_list1 = ["a","b","c","d","e","f"]
# Modifying list elements by index
my_list1[4] = "eraser"

my_list1 = ["a","b","c","d","e","f"]
# Delete an element by index
del my_list1[0]

my_list1 = ["a","b","c","d","e","f"]
# Delete list interval elements by index
del my_list1[0:3]

C # generic collection List

The list in python is equivalent to the array in C# and various usages are similar.

Array initialization:

int[] arrayInt = { 1, 2, 3, 4, 5 };

Array value:

int[] arrayInt = { 1, 2, 3, 4, 5 };

2-6 Yuanzu (list)

The difference between Python elements and lists

The difference between tuple and list is that the elements of list can be modified, and the elements of tuple can not be modified, so the elements can also be called immutable list.

The list is defined with brackets [] and tuples are defined with parentheses (). The syntax format of tuples is as follows:

# my_tuple is the name of tuple variable, which can be named arbitrarily
my_tuple = (Element 1,Element 2,Element 3...)

Other lists are similar to python

2-7 dictionary

python dictionary

Dictionary initialization:

# my_dict is a variable name
my_dict = {key1:value1,key2:value2......}

Get dictionary value:

my_dict = {"red": "gules", "green": "green", "blue": "blue"}

Addition, deletion and modification of Dictionary:

my_dict = {"red": "gules", "green": "green", "blue": "blue"}
my_dict["orange"] = "orange"
my_dict = {"red": "gules", "green": "green", "blue": "blue"}
my_dict["red"] = "Light red"
my_dict = {"red": "gules", "green": "green", "blue": "blue"}
del my_dict["red"]
my_dict = {"red": "gules", "green": "green", "blue": "blue"}

C# dictionary

Similarly, it is also the occurrence of key value pairs

2-8 set

Collection of python

Now to review:
Parentheses are used to declare tuples: my_tuple = (element 1, element 2, element 3...)
Brackets are used to declare the list: my_list = [“apple”, “orange”, “grape”, “pear”]
Braces are used to declare the dictionary: my_dict = {key1:value1,key2:value2…}

What about the assembly? Python also uses braces to declare collections. Of course, you can also create a set through the set function.

The syntax format of the set definition is as follows:

my_set = {1, 2, 3, 3, 10, 4, 5, 6}

Because the element formats of dictionaries and collections are different, python can recognize them automatically

Addition, deletion, modification and query of collection:

my_set = {"apple", "orange", "pear", "grape"}

my_set = {"apple", "orange", "pear", "grape"}
# An error is reported for the second deletion because apple is no longer in the collection

my_set = {"apple", "orange", "pear", "grape"}
my_set[0] = "watermelon"

my_set = {"apple", "orange", "pear", "grape"}

Common methods:
The usage is similar to that of the list, so we don't give more demonstrations

Set of C #

There are many types of collections in C #:
Dynamic array ArrayList, generic collection List, Hashtable, Queue, Stack

Addition, deletion, modification and query are similar, but the methods are different

2-9 function

python functions

Use the def definition, followed by the code snippet of the function

# Create a function
def show():
    print("I am a function with no parameters and no return value")

C #'s function

Have access rights, return parameters and parameters

public void show()
	print("Code snippet");

Python and C #'s functions are similar. Note that the python function () is followed by a colon.

OK, the basic syntax of python has been introduced

Keywords: Python C# OOP

Added by scoman on Fri, 04 Mar 2022 05:30:00 +0200