Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
28
14
109
8.6K
Sergei Kotov
Sergei Kotov@kotov_dev·
@PythonPr Answer: D) 1010 String repetition, not math! › a = "10" is a string (notice the quotes!) › a * 2 repeats the string twice › "10" * 2 = "1010" (not 20!) For beginners: Strings multiply by repeating! "ha" * 3 = "hahaha". Want math? Use int("10") * 2 = 20.
English
2
0
29
627
Earnest Codes
Earnest Codes@Earnesto037·
@PythonPr Answer: (d) 1010 The code initializes the variable a as a string with the value "10". The next line assigns the value of a to the variable b. The third line reassigns a to the result of a * 2. In Python, when you multiply a string by an integer, the string is repeated
English
1
0
5
267
Prem Prakash
Prem Prakash@4EverPrem·
@PythonPr 1010 Since 10 is enclosed within quotes, it is a string and not int. The asterisk when applied to strings, it multiplies the string with the multiplicand.
English
0
0
2
161
Biel Men
Biel Men@BielMenHaha·
@PythonPr Alternative D: "1010". The mult operation will repeat and interpret that a new instance of str should be created, repeating the str in int times, and making concatenation between their chars at the end of each other.
English
0
0
1
195
Paylaş