Introduction to python Basics

Posted by metrostars on Wed, 29 Dec 2021 06:40:22 +0100

catalogue

1, Getting started with python

2, python indent

3, Python comments

1) Single line note

2) Multiline comment

4, Python variables

1) Variable definition understanding

2) Variable name naming

3) Assign multiple values

4) Output variable

5) Global and local variables, global keywords

6) exercises

1, Getting started with python

I won't introduce the software installation. You can search the b station or Baidu to install python and pycharm

What is pycharm?

Pycham is the compiler of python. If we don't have a compiler, it's very difficult to write code. Moreover, pycham provides many plug-ins and beautiful interfaces, which makes you write code fast and comfortable, compared with writing on the IDE of python.
of course! Download software is not necessarily used in the official website, but also downloaded to the software housekeeper's official account. I am also a loyal fan of this official account!
Step 1: Print

Single quotation marks and double quotation marks have the same meaning in a certain sense, but double quotation marks have more permissions than single quotation marks

print('Hello World!')
print("Hello World!")
print("'Hello World!'")

Output:

Of course, you can print anything else you want, right?

print('Beautiful and handsome, I love you')

2, python indent

There are so many basic grammars of python that I should not be able to cover them all.

Here, we must thank the pycharm compiler. Why do you say that?

In python, it will automatically help us complete line feed, indent and other problems. It will not write code in the IDE. You should write it manually. If you forget to do it manually, you will report an error. Therefore, I do not recommend using only the python interpreter. You must use python!

Give some examples. Of course, if you are Xiaobai, you can only print now. Don't worry. You just need to look at it. I'll talk about it later.
For example, indentation refers to the space at the beginning of the code line. If the following statements need to be tap (of course, if you use the pycham compiler, it will automatically tap).
For example, the colon after the if statement will be left blank on the next line as soon as you hit enter, commonly known as indentation

if 6 > 2:
  print("Six is greater than two")


3, Python comments

Comments can be used to interpret Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code.


1) Single line note

Start the part requiring comments with#
For example:

#print("'Hello World!'")
print('Beautiful and handsome, I love you')

Or this:

#print("'Hello World!'")
print('Beautiful and handsome, I love you') #Beautiful and handsome, I love you

You can see the following. This is the meaning of annotation. See.

2) Multiline comment

The first method (not recommended, too troublesome)

#print('Hello World!')
#print("Hello World!")
#print("'Hello World!'")
print('Beautiful and handsome, I love you')

You can also left click to select the code we need to comment, release, and press: Ctrl +?, The same effect annotation is completed.

# print('Hello World!')
# print("Hello World!")
# print("'Hello World!'")
print('Beautiful and handsome, I love you')

The second method:
Put the content to be annotated in three pairs of quotation marks., For example:

'''
print('Hello World!')
print("Hello World!")
print("'Hello World!'")
'''
print('Beautiful and handsome, I love you')

note off
Left click to select the code we need to uncomment, release, and press Ctrl +?


4, Python variables


1) Variable definition understanding

In Python, variables are created when you assign a value to it: Python has no commands for declaring variables. A variable is created the first time you assign a value to it.
For example:

x = 5
y = "Stupid child"
print(x)
print(y)

For the same variable, if the assignment type is different, the type will change, for example:

x = 4       # x is now plastic surgery
x = "Stupid child" # x is now a string
print(x)
print(y)

Print as:

If you want to specify the data type of a variable, you can do so through type conversion, such as:

x = str(3)    # x would be '3'
y = int(3)    # y will be 3
z = float(3)  # z will be 3.0

Well, I just talked about types. Xiaobai certainly doesn't know what types are and how to view them. Let's talk about how to obtain types. Here we need to use the type function, for example:

x = 5
y = "Stupid child"
print(type(x))
print(type(y))

See what is returned: int is an integer and str is a string. That's what you have to remember.

String variables can be declared in single or double quotation marks:

x = "Stupid boy, come on!"
y = 'Stupid boy, come on!'
print(x)
print(y)

Return to:

Variable names are case sensitive:

a = "Stupid boy, come on!"
A = 'Stupid boy, come on!'
print(a)
print(A)

Return to:

 
2) Variable name naming

Variables can have a short name (such as x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

  • Variable names must start with letters or underscore characters
  • Variable names cannot start with numbers
  • Variable names can only contain alphanumeric characters and underscores (Az, 0-9, and)
  • Variable names are case sensitive (age, age, and age are three different variables)

For example:

myvar = "Stupid boy, come on!"
my_var = "Stupid boy, come on!"
_my_var = "Stupid boy, come on!"
myVar = "Stupid boy, come on!"
MYVAR = "Stupid boy, come on!"
myvar2 = "Stupid boy, come on!"

Naming convention:
Camel rule: every word begins with a capital letter except the first word

myNameIs = "Stupid boy, come on!"

Pascal's Law: every word begins with a capital letter

MyNameIs = "Stupid boy, come on!"

Snake rule: each word is separated by an underscore character

My_Name_Is = "Stupid boy, come on!"


3) Assign multiple values

Multiple values of multiple variables.
Python allows you to assign values to multiple variables on one line:

x, y, z = "Stupid kid 1", "Stupid child 2", "Stupid child 3"
print(x)
print(y)
print(z)

Output:

Note: make sure that the number of variables matches the number of values, otherwise you will get an error.  

One value of multiple variables
You can assign the same value to multiple variables in a row:

x = y = z = "Stupid child"
print(x)
print(y)
print(z)

Output is:

Open a collection
If you have a set of values in a list, tuple, etc. Python allows you to extract values into variables. This is called unpacking. Of course, here, you may not know what the set list and tuple are. It doesn't matter. I'll talk about it later.

fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)


4) Output variable

The print statement is usually used to output variables.
To combine text and variables, Python uses the + character. Why not? Because of the following:

x = "Stupid child"
print("Handsome guy is" + x)
print("Handsome guy is",x)

Output is:

You can also use the + character to add one variable to another:

x = "Stupid children are so stupid"
y = "Handsome"
z = x + y
print(z)

Return the same:

For numbers, the + character is used as a mathematical operator:

x = 6
y = 10
print(x + y)

return:

If you try to combine a string and a number, Python will give you an error, such as:

x = 5
y = "Stupid child"
print(x + y)

 

How to modify it? Just convert the number to character, then modify it to:

x = 5
y = "Stupid child"
print(str(x) + y)

Successful return:


5) Global and local variables, global keywords

Variables created outside the function are called global variables. You can use global variables, both inside and outside the function. Of course, here, you may not know what a function is. I'll talk about it later. Don't panic.

x = "Stupid child"
def myfunc():
  print("Handsome guy is " + x)
myfunc()

Return to:

If you create a variable with the same name inside a function, the variable will be a local variable and can only be used inside the function. Global variables with the same name remain as is and have the original value.

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

Return to:

Global keywords
Usually, when you create a variable inside a function, the variable is local and can only be used inside the function. To create a global variable within a function, you can use the global keyword.

x = "handsome guy"

def myfunc():
  global y
  y = "yes"

myfunc()

print("Stupid child" + y + x)

Return to:


6) Exercises

  1. Create a variable named carname and assign a value to it.
  2. Create a variable named x and assign it a value of 60.
  3. 5 + 10 uses two variables x and y. Display and print their sum
  4. Create a variable named z, x=8,y=9, assign x + y to it, and display the results.

Topics: Python Pycharm IDE