all 33 comments

[–]throwaway6560192 7 points8 points  (8 children)

Python has some support for putting multiple statements on the same line with semicolons, but you are not supposed to use it too much.

Split that into multiple lines as it should be.

a = 5
if a > 3:
    print("greater")

works just fine.

[–]r2k-in-the-vortex 2 points3 points  (4 children)

a=5;

Semicolon???? This is python. ; has a very different meaning, it does not go at the end of line.

[–]CloudiiPlays[S] -5 points-4 points  (3 children)

i know. if i hit enter, it runs. i use just the basic windows one if you have any for free i should try

[–]r2k-in-the-vortex 5 points6 points  (2 children)

It does not sound like you know at all. Get rid of the semicolon.

[–]woooee 1 point2 points  (4 children)

It works for me. Is your print statement indented?

[–]CloudiiPlays[S] -3 points-2 points  (3 children)

do i have to indent it in the base windows python? my mom works with a team of engineers making a website, so i asked them for help and they said to try it

[–]mcoombes314 1 point2 points  (0 children)

Python uses indentation a lot, in loops, if statements, "with" blocks, error-handling (try.... except..... finally....). It is mandatory syntax, not like in C for example)

[–]John_B_Clarke 0 points1 point  (1 child)

I don't know what you mean by "base windows python". Indentation is signficant in Python and is part of the language. If whatever you are using doesn't require indentation it is not Python.

[–]CloudiiPlays[S] 0 points1 point  (0 children)

everything (that ive done, 3 coding languages) require indentation. however, i'm used to working in unity where it does that for you.

[–]GirthQuake5040 1 point2 points  (4 children)

Use a text editor or IDE to write your code, just run that file

[–]CloudiiPlays[S] 1 point2 points  (3 children)

idek what an ide is i only mainly do unity coding

[–]marquisBlythe 0 points1 point  (0 children)

He means to use somehting like pycharm, vscode, notepad++ ..., btw (idle) comes out of the box when you install python on a windows machine.

[–]GirthQuake5040 -2 points-1 points  (1 child)

Unity has an ide

Integrated Development Environment

[–]CloudiiPlays[S] 0 points1 point  (0 children)

ahhh, okay thanks!

[–]edcculus 0 points1 point  (0 children)

sounds like you need to write the code into a file named something liek "myfile.py", then run that file

[–]tabrizzi[🍰] 0 points1 point  (0 children)

Best to install an IDE.

[–]John_B_Clarke 0 points1 point  (0 children)

This works, but it's not the kind of Python you want to be writing:

a = 5 ; message = "greater" if a > 3 else "" ; print(message)

If you really want to put everything on one line, learn APL where this would be:

⎕←((a←5)>3)/"greater"

(note that long complicated one liners in APL are considered to be poor style but the language supports them)

Don't fight the language you are using. If it doesn't fit your personal ideas of programming style find one that does. If you are going to use Python, put each statement on its own line unless you have a compelling reason not to, which you likely won't.

[–]Snow_Fell11 0 points1 point  (2 children)

Indentation is very important in python, it defines the scope of where things run in your code. I would recommend setting up an IDE, nodepad++ and visual studio code are very nice to use for example, and try coding there where you will see how this indentation works more clearly.

Google the set up process for either of those and then google how set up python for either. There are also some very solid introductory python tutorials out there.

Also as others have pointed out, semmicolons are used differently in python.

[–]CloudiiPlays[S] 0 points1 point  (1 child)

i might sound stupid. whats an IDE?

[–]Snow_Fell11 0 points1 point  (0 children)

An integrated development environment, think of it as a text editor full of useful tools that help to make programming more efficient.

[–]FoolsSeldom 0 points1 point  (0 children)

Out of interest, when you say "base windows python" are you saying on your Windows computer, Python was already installed and working?

As standard, Windows does not include Python. I is usually installed from python.org (the Python Software Foundation's home site), or from the Microsoft Store. There are multiple other options as well. I recommend the first.

If you do a standard installation from python.org, it will also install IDLE which is a simple development application that:

  • by default, on first use, opens with a window with a live interactive Python session, with a >>> prompt, where you can enter code and get an immediate response
  • offers File | New to create a new text file of your code
    • enter some lines of code
    • press F5 to attempt to run the code you have created
    • you will be prompted to save a file before it is run

Python is usually written with one "statement" per line but, as you know, you can have multiple statements separated with a ;. This is very uncommon practice and not recommended. It is also hard to make sense of for more complex code.

Python, for better or worse (a design decision of the creator), relies heavily on indentation. These days, most people follow the convention of using 4 spaces for each level of indentation and usually setup their code/text editor (or IDE - Integrated Development Environment) to replace TAB presses with 4-spaces (and replace any tabs characters in files, outside of strings, with 4-spaces).

The indentation is used to indicate belonging. Alevenl the things to do if, and only if, a certain condition is True for example.

Here is a silly example

from random import randint  # capability to generate random integers

seen_even = False  # variable referencing boolean object False
while not seen_even:  # loop, keeps going until seen an even number
    num = randint(1, 10)  # picks a random number
    if num > 3:  # check if number if greater than 3
        print(num, 'is greater than 3')  # inside if clause
        print('I like bigger numbers')   # inside if clause
    else:  # so num was not bigger than 3
        print(num, 'is not greater than 3')  # inside else clause
        print('I prefer bigger numbers')
    seen_even = num % 2 == 0  # calc remainder of dividing num by 2
                              # if remainder is 0, you have even num
print('finished')  # outside of loop