Suppose I use this block of code very often in my Python file
if x == 1:
x += 1
else:
x -= 1
Is there a way to “copy” this code, so that I don’t have to type these four lines repeatedly throughout the file?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes define yourself a function for it:
Then you can call the function from anywhere in your code instead of rewriting the same logic.