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

Programming Language : Python Question a)Where and how do you get a program development environment for...
Programming Language : Python Question a)Where and how do you get a program development environment for this language? b) Are there any IDEs that support this language? If yes, where and how do you get it?
Write a program in Python language which will do the following: Write a program to prompt...
Write a program in Python language which will do the following: Write a program to prompt the user to enter a company name, city, state and zip code. Create a dictionary with the data. Output the dictionary. Then remove the city from the dictionary and output again.
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...
write a Python program (with comments) to do the following: Define a list list1 = [['Rose',...
write a Python program (with comments) to do the following: Define a list list1 = [['Rose', 'Daisy'], 7, 0, 5.5, [1, 22, 2.45, 3, 6, 5.8]] and print it. Print the length of list1 (i.e. number of items in list1) with a suitable message. Print the first character of the first string in the first item of list1 with a suitable message (HINT: The first item is the item with index 0). Print the sum of the last 4 numbers...
Fill in needed code to finish Mastermind program in Python 3.7 syntax Plays the game of...
Fill in needed code to finish Mastermind program in Python 3.7 syntax Plays the game of Mastermind. The computer chooses 4 colored pegs (the code) from the colors Red, Green, Blue, Yellow, Turquoise, Magenta. There can be more than one of each color, and order is important. The player guesses a series of 4 colors (the guess). The computer compares the guess to the code, and tells the player how many colors in the guess are correct, and how many...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
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.
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 find lattice structures
how do you find lattice structures
[PYTHON] How do you write a program that first gets a list of floating point numbers...
[PYTHON] How do you write a program that first gets a list of floating point numbers from input. The input begins with an integer indicating the number of numbers that follow. Then input all data values and store them in a list [PYTHON]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT