Python essay (i), Python basics

Source: Internet
Author: User

set your own template under Pycharm:

In the file---settings---The file and Code Templates---The Python script, add:

#!usr/bin/env python  

#-*-coding:utf-8 _*-

"""

@author: ${user}

@file: ${name}.py

@time: ${year}/${month}/${day}

"""

One, the first Python program:

< Span style= "Color:rgb (128,128,128); font-style:italic;" >#!usr/bin/env python 
#-*-coding:utf-8 _*- 
""
@author: Administrator
@file: helloworld.py
@ Time:2017/11/{day}
""
print ( "HelloWorld!!!" )
Hello, world. ")

Second, variable and assignment:

#-*-Coding:utf-8 _*-


@file: bianliang.py

"""
#赋值
Name = "Chenjisong"
Age = 30
Print (Name,age)

The string type must be quoted

A = 3
b = A
A = 5
Print (A, B)

Returns the result as (5,3)

Parse: A = 3, the memory address points to 3,b = A, then B = 3, at which point A and B are pointing to memory address 3, when a = 5, a memory address points to 5, then A = 3 the memory address is recycled, but B's memory address is not reclaimed, B is still equal to 3, so the final result returned is (5,3 )

The principle of variable naming:

1, display, easy to understand

2, Hump writing (first letter capitalization) Example: NUMSOFJACKGF

3, the lower line writing (not for the middle horizontal line) For example: NUMS_OF_JACK_GF

4. Cannot start with a number, but can be in the middle and end

5. Cannot have special characters in naming

6, the name of the variable can not have spaces

7. Keywords cannot be declared as variables

Verification of memory address:

C:\users\administrator>python

Python 3.6.3 (V3.6.3:2c5fed8, Oct 3, 18:11:49) [MSC v.1900-bit (AMD64)]

On Win32

Type "Help", "copyright", "credits" or "license" for more information.

>>> Import keyword

>>> A = 5

>>> B = A

>>> ID (a), id (b)

(1363763552, 1363763552)

A and B have exactly the same memory address.

>>> A = 10

>>> ID (a), id (b)

>>> (1363763712, 1363763552)

When the value of a changes, the memory address of A is also changed (is the memory address in Python, not the memory address of the physical machine)


Third, user interaction


[[email protected] ~]# python
Python 3.6.3 (Default, Nov 12 2017, 04:07:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on Linux
Type "Help", "copyright", "credits" or "license" for more information.
>>> name = input ("Please input your name:")
Please input your Name:chenjisong

>>> Print (name)

Chenjisong

>>> A = 5
>>> eval (' a ')
5

Iv. Conditional judgment and indentation

IF .... else and indent


Pseudo code:

If you are rich second generation

We're both dating.

Or you're working hard.

We can try it in contact.

Otherwise

No talk.

 
indent to be consistent:
Sex = input ( "Plsase Input your Gender:" )
if sex = = "Gril" :
   print ( "I would like to having a baby" )
elif sex = = "man" :
   print ( "going to homesexual!" )
print ()

Game: Guess lucky numbers:

#!usr/bin/env python
#-*-Coding:utf-8 _*-
"""
@author: Administrator
@file: lucky_number.py
@time: 2017/11/18
"""
Lucky_number= 18
Guess_number= int(input("Can you guess my lucky_number:"))
ifGuess_number>Lucky_number:
Print("Guess_number is bigger and then Lucky_number")
elifGuess_number<Lucky_number:
Print("Guess_number is smaller and then lucky_number:")
Else:
Print("Congratulations,you guess it,but no prize")


V. Cycle control:

Break End Loop: (guess right, jump out of the loop, guess right.)

While True:
Lucky_number= 18
Guess_number= int(input("Can you guess my lucky_number:"))
ifGuess_number>Lucky_number:
Print("Guess_number is bigger and then Lucky_number")
elifGuess_number<Lucky_number:
Print("Guess_number is smaller and then lucky_number:")
Else:
Print("Congratulations,you guess it,but no prize")
Break
 whileLucky_number!= Input_num:
Input_num = int(input("Input The Guess num:"))
ifInput_num >Lucky_number:
Print("The real number is smaller")
elifInput_num <Lucky_number:
Print("The real number is bigger")
Else:
Print("Bingo")


Six, the number of cycles limit:


#-*-Coding:utf-8 _*-


@file: lucky_number.py

"""
Lucky_number = 18
Input_num=-1
Guess_count = 0
#while Lucky_number! = Input_num:
While Guess_count < 3:
input_num = Int (input ("Input The Guess num:"))
Print ("Guess count:", Guess_count)
If Input_num > Lucky_number:
Print ("The real number is smaller")
Elif Input_num < lucky_number:
Print ("The real number is bigger")
Else
Print ("bingo!")
Break
Guess_count + = 1
Else
Print ("Try Too many times")

Dual judgment:

First weight: Three guesses no direct exit (GUESS_COUNT>3), print "Try Too many times"

Second weight: guess right. Print bingo directly, exit

For loop guess number game:

#!usr/bin/env python
#-*-Coding:utf-8 _*-
"""
@author: Administrator
@file: lucky_number.py
@time: 2017/11/18
"""
#while True:
Lucky_number= 18
Input_num=-1

forIinchRange(5):
Input_num= int(input("Input The Guess num:"))
ifInput_num>Lucky_number:
Print("The real number is smaller")
elifInput_num<Lucky_number:
Print("The real number is bigger")
Else:
Print("Bingo!")
Break
else
:
Print("Try too many times")


Vii. Common data types

Data type:

Digital:

int (integral type)

Float (float type)

Long (integer)

Boolean: (True (1) and False (0)) True and False

String str

Lists List

Ganso tuple

Dictionary Dict

Type to view the data type:

>>> type (2**10)

<class ' int ' >

>>> Type (2.99)

<class ' float ' >




This article from the "Linux" blog, reproduced please contact the author!

Python essay (i), Python basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.