syntax

Python syntax: Could colons be optional when at end of line?

Python syntax: Could colons be optional when at end of line? Question: I would like to understand whether it would be possible to change the syntax of Python making the colons of compound statements optional when they are at the end of the line, apart from whitespace and comments. E.g.: if so() print(“yes”) else print(“no”) …

Total answers: 2

Python colon not a sequence of numbers?

Python colon not a sequence of numbers? Question: In r code, the sequence of integers from 1 to 4: 1:4 How can I replicate this in python 3? The use case is selecting from a pandas dataframe using iloc: df.iloc[1:4] Asked By: DDDD || Source Answers: In Python, in order to generate a range of …

Total answers: 5

How to disable error syntax highlight Sublime Text 3

How to disable error syntax highlight Sublime Text 3 Question: I am using a language I made with a similar syntax to python, and I wanted to use python syntax highlighting for my language as well. The only problem is that my language uses curly brackets rather then : and indents. So some times when …

Total answers: 2

The difference between double brace `[[…]]` and single brace `[..]` indexing in Pandas

The difference between double brace `[[…]]` and single brace `[..]` indexing in Pandas Question: I’m confused about the syntax regarding the following line of code: x_values = dataframe[[‘Brains’]] The dataframe object consists of 2 columns (Brains and Bodies) Brains Bodies 42 34 32 23 When I print x_values I get something like this: Brains 0 …

Total answers: 4

Python [Invalid syntax] with async def

Python [Invalid syntax] with async def Question: I am trying write discord bots using Python, I have come across and threw together this bot. import discord import asyncio import random client = discord.Client() inEmail = input(“Email:”) inPassword = input(“Passwd:”) async def background_loop(): await client.wait_until_ready() while not client.is_closed: channel = client.get_channel(“************”) messages = [“Hello!”, “How are …

Total answers: 4

What is `1..__truediv__` ? Does Python have a .. ("dot dot") notation syntax?

What is `1..__truediv__` ? Does Python have a .. ("dot dot") notation syntax? Question: I recently came across a syntax I never seen before when I learned python nor in most tutorials, the .. notation, it looks something like this: f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8)) # prints 0.125 I figured …

Total answers: 4

SyntaxError invalid token

SyntaxError invalid token Question: I have a problem when I try to assign a value to a variable. The problem shows up when I try to put a date as a tuple or a list in this order: year, month, day. >>> a = (2016,04,03) # I try to put the date into variable ‘a’ …

Total answers: 5

Why are unparanthesized tuples in generators not allowed in the expression field?

Why are unparanthesized tuples in generators not allowed in the expression field? Question: # why is the following invalid x = (k, v for k, v in some_dict.items()) # but if we wrap the expression part in parentheses it works x = ((k, v) for k, v in some_dict.items()) After reviewing the documentation, I couldn’t …

Total answers: 2

Apply function for objects from "yield from" statement in python

Apply function for objects from "yield from" statement in python Question: # currently I have this code def some_func(): for match in re.finditer(regex, string): yield other_func(match) I was wondering if there was a way to squash it into one line # looking for something like def some_func(): yield from other_func(re.finditer(regex, string)) Asked By: AlanSTACK || …

Total answers: 2

How to initialize datetime 0000-00-00 00:00:00 in Python?

How to initialize datetime 0000-00-00 00:00:00 in Python? Question: I’ve been trying to use the datetime library in python to create a datetime object with this value: ‘0000-00-00 00:00:00’. However, I keep getting an error that the year is out of range. How can I properly initialize this? Asked By: user3802348 || Source Answers: There …

Total answers: 5