Basic input and output
Output
c:printf ("Hello world\n");
P:print ("Hello world\n")
Simple string output for the printf ()/print () function, the C and Python differences seem small, and the attentive reader may find that there is no semicolon behind a statement in Python, and it is true that in Python, the general line is a single statement, and if you write two, You need to add; Separated, but generally no one will go to do so, think about how many times in C is because you forget to add a semicolon cause the compilation error, so in Python, forget the semicolon.
But a more complex format output, such as a number or string that prints a certain amount of precision, is different.
C:
#include<stdio.h>int main(){int a=1;float b =1.0;char str[10]="abc";printf("a=%d\tb=%.3f\tstr=%s",a,b,str) ;return 0;}
The result should be a=1 b=1.000 STR=ABC
P:
1.0str="abc"print ("a=%d\tb=%f\tstr=%s" % (a, b,str))print(‘a={}\tb={}\tstr={}‘.format(a, b,str))
Result is
- A=1 b=1.000000 STR=ABC
- A=1 b=1.0 STR=ABC
Python also has a format output similar to the C language, which is to change the comma to% and enclose the variable in parentheses. In addition, there is the format method.
What the format method does in Python is to replace each parameter value with the location where the format is located. There are four types in Python3: int, float, bool, complex,int are expressed as long integers, no long in Python2, and no char type.
As you can see, the variables in Python do not need to be declared, but each variable must be assigned before it is used, and the variable will not be created until the variable is assigned. This is different from C, other such as escape sequence \n\t ... All the same. Also, in Python, the print () function defaults to a newline character as the end value, and the printf () function ends in the C language and requires a manual line break. If you don't want to wrap in Python, you can add end = "" After you say it, and be careful to separate the lines with commas. Such as:
print(‘a={}\tb={}\tstr={}‘.format(a,b,str),end =‘‘)print(‘a={}\tb={}\tstr={}‘.format(a,b,str),end =‘‘)
The result is a=1 b=1.0 str=abca=1 b=1.0 str=abc
Input
C:
#include <stdio.h>int Main () {int a= 0; float B=0; char str[10]; printf ( "Please input three data, the input three data will be assigned to A,B,STR, separated by commas \ \"); scanf ( Span class= "hljs-string" > "%d%f %s ", &a,&b,str); printf ( "%d\t%f\t%s\t", a,b,str); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
The result of the operation is:
- Please input three data, input three data will be assigned to A,B,STR, separated by commas input: 1 2.0 GG output: 1 2.000000 GG
P:
a,b,str=input("请输入三个数据,输入的三个数据将会分别赋值给a,b,str, 用逗号隔开\n").split()print(‘a={}\tb={}\tstr={}‘.format(a,b,str),end =‘ ‘)
The result of the operation is:
- Please input three data, input three data will be assigned to A,B,STR, separated by commas
- Input: Test1 test2.0 22
- Output: A=test1 b=test2.0 str=22
The input () function is used to get some text from the user, there is a string parameter to prompt the user to enter information, in the C language, to achieve the same function, you need printf (). Of course input can only accept a value, in order to enter more than one, with input (). Split (), you can add any number of data in front of it.
It is worth noting that input returns a string, although looking at a number, but actually a string, which requires an int (input ()) to convert, if the input is not a number but a string, the conversion will be error, in the place of dealing with the user input, pay attention to check whether the type matches, You can use a try statement, and then say it later.
Control flow Statements
There are three control flow statements in Python--if for and while, and the following comparisons differ from the C language
If
C
#include <stdio.h>int main () {int a=0; printf ( "Please enter a number \ n"); scanf ( "%d", &a); if (A==0) {printf (" a = 0 "); } else if (A>0) {printf (else if (A<0) {printf (else printf ( "A is not a number"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
P
a=int(input("请输入一个数字\n"))if a==0: print("a =0") elif a>0: print("a>0") else: print("a<0 ")
Run as results
- Please enter a number
- 23333
- A>0 Strips
C: The conditional statement is followed by parentheses to include the condition, the statement block when the condition is set with the curly brackets {} Around, Python, if statement after the condition followed by a colon, the statement block executed when the condition is consistent indentation, generally indented four spaces or a tab.
But be sure to remember that Python uses indentation levels to tell the compiler which statements belong to which block respectively. If the indentation in the same statement block is inconsistent, the result of the program may not be the same as the expected result or even a direct error, which is the disadvantage of not using the curly brackets {}.
As for the else if () shorthand, the switch statement does not exist in Python elif. You can do this by using the IF: Elif. Else statement to accomplish the same thing.
While
While statement repeats a block of statements when the condition is true
C:
#include <stdio.h>int main () {int a=233,b;BOOL Flag=1;printf ( "guess a equals how much, just enter a number \ n"); scanf ( "%d", &b); while (flag) {if (a==b) {printf ( "guess right \ n"); Flag=0;} else {if (a>b) {printf ( "a little bit small \ n"); } else {printf ( "a bit too big \");} printf ( "continue to guess:"); scanf ( "%d", &b);}} return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
P:
a=233flag =1b=int (Input ( "guess a equals how much, enter a number randomly \ n ")) while flag: if a==b: print ( Span class= "hljs-string" > "guessed right") Flag=0 else: if a>b: print (" a little bit small ") else: print ( "a bit too big") B=int (Input (else: print ( ' game over. ')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
The results are as follows
Guess a equals how much, just enter a number 2 a little bit.
Keep guessing 3 's a little small.
Keep guessing 255 's a little big.
Keep guessing 233 guess the game is over.
While in C and Python, the difference is that the parentheses () of the judging condition are replaced by a colon: the curly Bracket {} is indented, and the while in Python can be used in conjunction with else, and the else code block starts executing when the condition of the while loop becomes false. You can also use the Else statement. Other usages are not very different.
For
C:
#include <stdio.h> int main() { int i; for(i=0;i<10;i++) { printf("%d\t",i); } return 0; }
P:
for i in range(0,10,1): print(i,"\t",end="")
The output is as follows
The first parameter of the range () function specifies the starting position, the second parameter bounds the range, and the third parameter is the step. Range () generates a number at a time.
The In operator returns False if the value found in the specified sequence returns True, so it can also be used in other conditional statements, where a series of members, including strings, lists, or tuples, are included in the test instance. As to what is a list or this tuple, it is described later.
The for and for in Python and the while in C are not much the same as the while in C, and the brackets () in C are converted to colons: the {} of the loop body is replaced by a consistent indentation.
Like friends can add QQ Group, the group has free information for everyone to exchange learning
From c to Python