Question

In: Computer Science

Python uses indention in its program structures. How do you define the syntax of a language...

Python uses indention in its program structures. How do you define the syntax of a language construct that uses indention? Give an example to describe.

Solutions

Expert Solution

Python programming language uses indentation as its building block of syntax.

As seen in maximum of other programming languages, flower brackets are used to limit or define the scope of code. For example scope of function or scope of if-else block or scope of for loop.

But in python it is entirley different. It follows indentation to define the scope of its code parts.

Indentation means proper spacing.

For example i will give an example of for loop in both java and python languages. Iam adding java code to let you understand the syntax difference between python and other major programming languages.

code to print Hi 10 times
In java

for(int i=0;i<10;i++){

System.out.println("Hi");

}

see in above code, java has braces to define its scope to for loop.

In Python

for i in range(10):
print("Hi")
print("Bye")

  
See in above code, python has indentation rule to define its scope to for loop.
You can see a space infront of print in the second line. It keeps the print("Hi") statement inside the for loop.
print("Bye") has no space infront of it and it is relativly placed with for loop. So it wont be inside for loop. This statement will be print into console after printing "Hi" 10 times.

Above given example was simple one.

Now i will show you example of if-else block inside a for loop.

for i in range(10):
if i%2==0:
   print("Even")
   else:
   print("Odd")

      
In the above code you can see if-else statement inside for loop.

If statement is placed after giving a proper space inside for loop, which must be exactly followed by else statement. We can easily construct syntax in python keeping indentation and proper spacing and relative place of statement starting point.

Feel free to ask if you find any difficulty
Please upvote if this answer is helpfull.


Related Solutions

Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
Python Language: The Marietta Country Club has asked you to write a program to gather, then...
Python Language: The Marietta Country Club has asked you to write a program to gather, then display the results of the golf tournament played at the end of March. The Club president Mr. Martin has asked you to write two programs. The first program will input each player's first name, last name, handicap and golf score and then save these records in a file named golf.txt (each record will have a field for the first name, last name, handicap and...
How do you define undamped, underdamped, critically damped and overdamped structures? Derive the necessary equations and...
How do you define undamped, underdamped, critically damped and overdamped structures? Derive the necessary equations and draw necessary pictures.
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep to tell whether the participant are of the ages through 18 to 60 and sleep less than 6 hours per day? 2. Develop codes to check whether the sleep function you defined can make correct judgement. Make sure you write comments or create informative vairable name so that I can understand how you check the sleep function. (Hints: You can create toy data/dataframe to...
how do you find lattice structures
how do you find lattice structures
Must use AT&T x64/GNU Assembly syntax. Write an assembly language program that reads in two integers,...
Must use AT&T x64/GNU Assembly syntax. Write an assembly language program that reads in two integers, A and B, and uses them to compute the following expressions. You must use the same values for A and B throughout all three expressions. 1) A * 5 2) (A + B) - (A / B) 3) (A - B) + (A * B)
Using MIPS assembly language In this program, you should define an array of 10 elements in...
Using MIPS assembly language In this program, you should define an array of 10 elements in your data segment with these values: ? = {11, 12,−10, 13, 9, 12, 14, 15,−20, 0} a. Write a function which finds the maximum value of this array. b. Write another function which calculates the summation of this array. c. Call these functions in your main program, and print the outputs of these functions to the user i. “The maximum is 15” ii. “The...
How do I make a dictionary in Python language? Can somebody please provide me with an...
How do I make a dictionary in Python language? Can somebody please provide me with an example code of a Dictionary in Python Language? Thank you in advance.
write a python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
Write a program in python language, which accepts 2 numbers and a + sign (for addition)...
Write a program in python language, which accepts 2 numbers and a + sign (for addition) A sign - (for subtraction) A sign * (for multiplication), / (for division) Then calculate and to display the result of the operation he chose with the two numbers. Displaying the appropriate message
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT