Should we use semicolon at the end of variable names in python? - python-2.7

In the code below, should we use semicolon at the end of assignment to num or not? Why?
# Python code to check whether a number
# is even or odd using bool()
def check(num):
return(bool(num%2==0))
# Driver Code
num = 8;
if(check(num)):
print("Even")
else:
print("Odd")

Many programming languages like C, C++, Java and others use semicolons to separate statements. In Python the semicolons are optional for single statements in a line. They are only mandatory, if you want write multiple statements in the same line. Some Python programmers write semicolons at the end of the line, because they are so used to it from other languages.
Thus, there is no need for the semicolon after num = 8, and you should avoid it, because it's unusual.

Semicolon is not required in your variable(s), but if you put them in one line, it will be more readable. Consider this x=2 y=3 and x=2; y=3

Related

List of special characters in SAS macros

I'd like to know which characters are safe for any use in SAS macros.
So what I mean by special characters here is any character (or group of characters) that can have a specific role in SAS in any context. I'm not that interested in keywords (made of a-z 1-9 chars).
For example = ^= ; % , # are special (not sure if # is actually used in SAS, but it's used for doc so still count as a parameter that is not 'safe for all uses').
But what about $ ! ~ § { } ° etc ?
This should include characters that are special in PROC SQL as well.
I'd like to use some of these characters and give them a special meaning in my code, but I'd rather not conflict with any existing use (I'm especially interested in ~).
A bit of general reference:
reserved macro
words
Macro word rules
SAS operators and mnemonics
Rules for SAS names
I think the vast majority of the characters on a standard English keyboard are used somewhere or other in the SAS language.
To address your examples:
$ Used in format names, put/input statements, regular expression definitions...
! 'or' operator in some environments
~ 'not' operator
§ Not used as far as I know
{} Can be used for data step array references & definitions
° Not used as far as I know
None of the above do anything special in a macro context, as Tom has already made clear in his answer.
Maybe SAS Operators in Expressions can help you for ~,
looking at the tables
Comparison Operators and
Logical Operators
The main triggers in macro code are & and % which are used to trigger macro variable references and macro statements, functions or macro calls.
The ; (semi-colon) is used in macro code (as in SAS code) to indicate the end of a statement.
For passing parameters into macro parameters you mainly need to worry about , (comma). But you will also want to avoid unbalanced (). You should avoid use = when passing parameter values by position.
You can protect them by adding quotes or extra () around the values. But those characters become part of the value passed. You can use macro quoting to protect them.
%mymac(parm1='1,200',parm2=(1,200),parm3=%str(1,200),parm4="a(b")
Equal signs can be included without quoting as long as your call is using named parameters.
%mymac(parm1=a=b)
In addition to the previous answers;
% is also used to include files in your program. %include.
Using special characters may cause your code to get stuck in a loop due to unbalanced quotes. SAS Note.
If you run into this just submit the magic string below:
*';*";*/;run;

Semicolon usage in Promela

I am learning promela syntax for Spin Modal Checker. I encountered this simple piece of code.
int count;
active proctype count(){
if
:: count++
:: count--
fi
}
As I know semicolon is used to define end of statement. Can I use ; in the end of both count++ and count-- and after fi; Will it change the way program is behaving? I would be grateful for clearing this semicolon thing for me.
Semicolons in Promela are so-called separators.
From the reference:
The semicolon and the arrow are equivalent statement separators in Promela; they are not statement terminators, although the parser has been taught to be forgiving for occasional lapses. The last statement in a sequence need not be followed by a statement separator, unlike, for instance, in the C programming language.
So the answer to your question is: you don't need to put semicolons after count++, count--, or fi because they are the last statements. The parser will ignore if you put them there anyway.

removing single quotation marks from a string in SAS

I have a requirement to read the string with both single quotes and without quotes from a macro retrieve_context.
While calling the macro, users can call it with either single quotes or without quotes, like below:
%retrieve_context('american%s choice', work.phone_conv, '01OCT2015'd, '12OCT2015'd)
%retrieve_context(american%s choice, work.phone_conv, '01OCT2015'd, '12OCT2015'd)
How to read the first parameter in the macro without a single quote?
I tried %conv_quote = unquote(%str(&conv_quote)) but it did not work.
You're running into one of those differences between macros and data step language.
In macros, there is a concept of "quoting", hence the %unquote macro function. This doesn't refer to traditional " or ' characters, though; macro quoting is a separate thing, with not really any quote characters [there are some sort-of-characters that are used in some contexts in this regard, but they're more like placeholders]. They come from functions like %str, %nrstr, and %quote, which tokenize certain things in a macro variable so that they don't get parsed before they're intended to be.
In most contexts, though, the macro language doesn't really pay attention to ' and " characters, except to identify a quoted string in certain parsing contexts where it's necessary to do so to make things work logically. Hence, %unquote doesn't do anything about quotation marks; they are simply treated as regular characters.
You need to, instead, call a data step function to remove them (or some other things, but all of them are more complicated, like using various combinations of %substr and %index). This is done using %sysfunc, like so:
%let newvar = %sysfunc(dequote(oldvar));
Dequote() is the data step function which performs largely the same function as %unquote, but for normal quotation characters (", '). Depending on your ultimate usage, you may need to do more than this; Tom covers several of these possibilities.
If the users are supplying your macro with a value that may or may not include outer quotes then you can use the DEQUOTE() function to remove the quotes and then add them back where you need them. So if your macro is defined as having these parameters:
%macro retrieve_context(name,indata,start,stop);
Then if you want to use the value of NAME in a data step you could use:
name = dequote(symget('name'));
If you wanted to use the value to generate a WHERE clause then you could use the %SYSFUNC() macro function to call the DEQUOTE() function. So something like this:
where name = %sysfunc(quote(%qsysfunc(dequote(%superq(name)))))
If your users are literally passing in strings with % in place of single quotes then the first thing you should probably do is to replace the percents with single quotes. But make sure to keep the result macro quoted or else you might end up with unbalanced quotes.
%let name=%qsysfunc(translate(&name,"'","%"));

Why don't we add a semicolon (;) at the end of if/else?

In Rust, I have noticed that everything is an expression except 2 kinds of statements. Every expression that adds ; will become a statement. Rust's grammar wants statements to follow other statements.
So why don't we add ; at the end of an if / else "expression"? This is also an expression, so why don't we do this:
if true {
println!("true");
} else {
println!("false");
};
The most common answer in the discussion is that it looks more like what users coming from other languages expect, and there is no harm in allowing that syntax (since the result type is () thanks to semicolons in the branches).
I guess because it is a block expression in other languages like Java or Nginx-Conf a semicolon is only set after statements and not after blocks.

PHP, C++, etc. syntax explanation

Why in the most programming languages required to put a semicolon after statements but not after things like if elseif and else?
Do the compilers all look out for newlines? If that's true then why wouldn't they do that for all statements?
Am I missing something? It really doesn't make sense to me...
Usually the semicolon is required because the compilers ignore most whitespace. It's not needed after statements like if, elseif, else because it's not the end of the statement. Those are only complete statements when followed by a statement or block of statements.
Because compilers for those languages don't consider whitespace for statement termination. A statement has to be terminated some how and it's done with a semicolon. You can write all code (although it would be a horrible, horrible idea) on one line as long as you terminate statements correctly.
Some compilers ignore whitespace and use the semicolon to determine statements, like variable assignments vs. if {} code blocks.
Other languages, like python, use whitespace to find statements and if blocks.
It relates to the difference between statements and expressions. Languages like C require a block to contain a series of statements. Control flow structures like if and while are statements all on their own:
void foo() {
if (bar) {
/* ... */
}
while (baz) {
/* ... */
}
}
There are no semicolons needed here because everything inside foo() is a statement. The question is, what if you want an expression like bar() in a place where a statement is expected? The C grammar says you can do that by adding a semicolon after the expression (in other words, an expression followed by ; is a statement).
So, this isn't valid:
void foo() {
1 + 2
}
Because 1 + 2 is an expression. You need to turn it into a statement:
void foo() {
1 + 2;
}
To fully understand what's going on, it's important to note that a block (something in curlies like { foo(); } is also a statement, and that the grammar for if is something like:
if ( <condition> ) <statement> (else <statement>)?
This is why if statements can have blocks for bodies or single statements: a block is a single statement.
Why use ';' rather than '\n' to terminate a statement.
Very few languages uses white space as something that affects the semantics of a language (new line being white space). This is because it is very error prone for the developer (as the white space is for all intense invisible).
Look at languages that do use white space to affect the semantics and you will see the effect on the programmers (special tools are usually built to help align things etc). Lots of bald developers (male and female) who have torn out their hair if frustration because they inserted a space instead of a tab and this caused the loop to be exited early. :-)
Also by not using white space to alter semantic meaning of the language you can now use white space solely for the human readers of the language to try and format the code into a form that makes it easy to read (for the human) (or you can abuse the white space to hide meaning).
Why are not all statements white space
Its just that you need a way to check the end of statement.
Some things it is obvious where the end of the statement is and you do not need a an extra delimiter to allow the parser to detect the end of a statement.
A source code is a set of statements. We have to delimitate the statements, using delimitators. If we use the newline as the delimitator, we can't structure our codes. Very long lines will only be readable by scrolling. (to avoid scrolling, long lines usually are split.) For example:
ParserUtils.RefreshProperty(m_cfg.PORTAL_ID, ParserUtils.CreateHashFromUrl(strDetailLinkUrl), Convert.ToInt32(m_cfg.Type), strPrice, strAddress, strStreet, strPostCode, strFeatures, strDescription, strImgFile, strBedrooms, strReception, strBath, strStatus, strLink, strPropType, strOutside, strTenure, strKeywords, strFullText, strContactInfo, m_ieBrowser.URL);
is very ugly and instead of this, we split this line to several lines to make this more readable:
ParserUtils.RefreshProperty(m_cfg.PORTAL_ID,
ParserUtils.CreateHashFromUrl(strDetailLinkUrl),
Convert.ToInt32(m_cfg.Type), strPrice,
strAddress, strStreet, strPostCode, strFeatures, strDescription, strImgFile,
strBedrooms, strReception, strBath, strStatus, strLink, strPropType,
strOutside, strTenure, strKeywords, strFullText, strContactInfo,
m_ieBrowser.URL);
This would be impossible if newline was the delimitator. Ifs, whiles and fors would be a total mess if newline was the operator. Consider this code:
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
System.out.println("It can be divided by two");
}
{
System.out.println("It can't be divided by two");
}
}
If newline was the operator instead of the semicolon, this source code would be very ugly:
for (int i = 0
i < 0
i++) { if (i % 2 == 0) { System.out.println("It can be divided by two")
} { System.out.println("It can't be divided by two")
} }
This code is difficult to read, and it's logically valid as the delimitator. For example, my wife writes my tasks on a paper (an algorithm) like this:
Buy food(Bread, Meat, Butter),
Go and pay the taxes,
Call your mother, because she wants to talk to you
These tasks are separated by commas, but notice that parameters are separated by commas too. We have to make a difference between a comma as a parameter separator and a comma as a delimitator of tasks, because the computer is not as intelligent as human beings. As a conclusion, the separator of tasks is a bigger comma than the separator of parameters. That's why the delimitator of statements is a semicolon and the delimitator of parameters is a comma.