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 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...
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...
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.
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]
HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE # DEFINE 'all_keywords',...
HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE # DEFINE 'all_keywords', A LIST COMPRISED OF ALL OUR NEGATIVE SEARCH KEYWORDS AS REQUIRED BY THE PROJECT # FOR EACH ELEMENT IN 'full_list' (THE LIST OF LISTS YOU WOULD HAVE CREATD ABOVE THE PREVIOUS LINE) # INITIALIZE THE SET 'detected_keywords' TO EMPTY # DEFINE VARIABLE 'current_reviewer' FROM CURRENT ELEMENT OF 'full_list' BY EXTRACTING ITS FIRST SUB-ELEMENT,... # ...CONVERTING IT TO A STRING, AND THEN STRIPPING THE SUBSTRING...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT