Python has a basic entry program

Posted on

Replace {} in C + + with strict indentation, which is simple and beautiful.

Variables do not need the traditional definition, system inference, similar to auto

Naming rules: combination of upper and lower case letters, numbers, underscores and Chinese characters

Note: case sensitive, the first character cannot be a number, not the same as reserved words

Comment ා comment one sentence, three quotation marks annotate multiple lines. Interestingly, three quotation marks are also strings

33 reserved words: (case sensitive)

   and    elif     import    raise    global

  as      else      in      return     nonlocal

  assert   except          is      try     True

  break    finally   lambda     while    False

  class     for     not       with     None

  continue  from     or        yield

  def      if     pass      del



Please write a program to convert the user’s input Fahrenheit to centigrade, or convert the input centigrade to Fahrenheit.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

The conversion algorithm is as follows: (C represents centigrade, f represents Fahrenheit)‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

         C = ( F – 32 ) / 1.8‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

         F = C * 1.8 + 32
Tempstr = input ("please enter temperature value:)
if TempStr[-1] in ['F','f']:
    C = (eval(TempStr[0:-1])-32)/1.8
    Print ("the temperature after conversion is {: 2F} C". Format (c))
elif TempStr[-1] in ['C','c']:
    F = 1.8*eval(TempStr[0:-1])+32
    The converted temperature is {: 2F} F ". Format (f))
    Print (wrong input format)

1. Input() function: read string, parameter is prompt

2. eval() function: remove the quotation marks of strings and change them into corresponding types. This function is really amazing

3. Slice operation, open left and close right, Li [:: 2] can add step size finally, – 1 is inverse index

4. String format operation, very convenient in the string {}, followed by. Format(). :. 2F for character control, floating point number with two decimal places



Prime test

from math import sqrt
n = int(input("Please input number:"))
for i in range(2,int(sqrt(n))):
    if n %i ==0:
        print(f"{n} is Not a prime number.")
    print(f"{n} is a prime number.")

1. The range (a, B, step) function produces an iterative sequence with step size

2. F-strings format, treat parentheses as variables


pi =3.14159
print(f'pi ={pi:.2f}')

“Pi = 3.14”

Base conversion

'int:31, hex:1f,oct:37'

3. For else statement, else can be matched with for. If for normally executes 10000 loops, else will be executed.



Print a simple triangle

n=int(input("Please input number:"))
for i in range(n):


Compared with C + +, the string supports * operation, that is, repeated N times

Leave a Reply

Your email address will not be published.