How to end if statement in python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm learning python.
I have touched on other programming languages ​​for example:
PHP, HTML, C #, C ++ etc. But I could not understand Python a bit from the programming languages ​​I listed
Uses {}
When I try to start an if statement in Python I will have to start with : but I can not find a way to end it.
I mean, if the if statement is activated, some code must be executed, so I can not find a way to see which code will be executed when the if statement is activated.
I'm 11 years old.
This might be really bad question for python programers that because I'm currently learning python

In python what you have to understand about block of code, is just indentation. check the following example:
if 10 > 20:
print('Inside if statement')
print('Outside if statement')
And the same goes to loop, function, class,...
In c#, c++, c we use {} to define a block of code but in python is just indentation

Related

How to read this piece of code step by step? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
def func(num):
if num==1:
return 1
num=num-1
print(num)
z=func(num)*num
print(num)
return z
func(6)
I am a beginner in python study and it's hard for me to read recursive code like this.
Could you guys introduce the right step of reading this piece of code for me?
I am also a little puzzled that why the two sets of output are in contrary order.
Thanks.
Try using this site to visualize your python code in a step-by-step manner:
Python visualisation tool
This site helped me as a beginner to understand and debug my code.

User-Defined Functions and Learning to Think Like a Computer Scientist with Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am brand new to coding / programming and am starting with the foundations of Python. I've used a few different resources (Codecademy, Automate the Boring Stuff, and most recently, How to Think Like a Computer Scientist by Using Python).
So far my education has been good, albeit a bit slow to start (not used to thinking in computer terms quite yet!). But I've run into a problem with one of the lessons that I cannot replicate in my IDE. Please see the code below:
def printTwice(bruce):
print('bruce')
print('bruce')
The lesson states the output should be 'Bruce, Bruce' which makes sense logically. However, when I go to run the code I get the following:
===RESTART: /Users/owner/Documents/bruce.py======
So essentially just another line to start a new code from. I am writing this code in a new file and just cannot figure out what I'm doing wrong.
Any help is appreciated.
Thanks!
Add printTwice('bruce') on a new line after the function, with no indentation like so:
def printTwice(bruce):
print('bruce')
print('bruce')
printTwice('bruce')
This line will call your printTwice function, passing the value 'bruce' to the variable bruce, which is not used.

How do I properly start creating a programming language in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I've been thinking to make a programming language written in python but I have no idea where to start, and I really don't know the steps creating it.
You would probably start by first planning out your language. That should take a lot of time. Then in Python... you would write a parser that can understand the syntax of your language, and an interpreter that can take the results of the parser and perform the actions of the program.
Your language that is written in Python with Python in-turn being written in C is practically guaranteed to be very slow and will not succeed, but this could be a really fun thing to do as a learning or education experience.
You will likely want to look at Abstract Syntax Trees. This is the underlying structure that python is built on. Take a look here at the documentation: https://docs.python.org/2/library/ast.html
Using ASTs you can at least define the syntax of your language. You will still need to solve the problem of how to interpret it on a platform to get your code to execute.

How are super, pass, yield and other keywords are implemented in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was asked this question in a recent interview. And I know super is the way to get the super class, pass has no effect but to close a function or class declaration, yield is used in generator. But I have no idea how they are implemented in Python.
I searched and only get the answer for super: How is super() in Python 3 implemented?, anybody can answer for others?
super is not a keyword in python, it's a variable like int or range.
Keywords like pass, yield, def, if, ... are consumed by the parser to build an AST (Abstract-Syntax-Tree). You can play with the AST by yourself with the ast-module of python.
The AST is then turned in some lower level machine or virtual machine code by the code generator, ready for execution.

Why is Python strict about indentation? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This question is coming from this Stack Overflow question. Why is Python is designed to use correct indentation either space or tab? In this way, is it purposefully to serve any benefit?
Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.
Consider this code snippet, for example:
def f(x):
if x == 0:
print("x is zero")
print("where am i?")
If it weren't for indentation, we would have no way of indicating what code block the second print statement belongs to. Is it under the if statement, the function f or just by itself in the document?
There is no set width of a tab character, so Python has no way of knowing how many spaces to count a tab as.

Categories