python

techguide
5 min readAug 6, 2020

New to Python So you may understand some of these, Hello world to your screen Comments just use a hash symbol in front of whatever you write And you can use the hash symbol in front of many lines if you want to take them out Assignment so if you’ve got a variable like X if you want to give it a number like 7 You can just say x equals 7 or a string, here we’ve got number equals 7 which is the string 7 these are called types so the main types are If you like the text or strings and numbers are either Integers they are whole numbers like 1 to 10 or something like minus 1 Or if there’s a fraction decimal point like in currency for example you can use floating-point numbers called floats so in text use string STR and in numbers use integers int and floats There’s a way of writing the syntax when you’re coding and in Python is slightly different from other languages .So if we have look at the examples we have an if statement, and if that statement is true Then Python will run the next line, and it knows only to run that one line because we have the gap in that one line of code The same for the for loop.

so you don’t just have one indentation you can have more in this example of a function We have a for loop so that has starts of the first indentation point inside the for loop is an if statement so that starts at the second indentation point and Inside the if statement we have a print statement and that starts at the third Indentation point so we can see which code is inside which other code So how does Python know when the code stops being inside We write the next line without any indentation So for example if we have a for loop And we have a print statement, then it will continue to print that and once the for loop finishes We see another print statement that has no indentationso if you try to add one plus one if they’re not both integers And one of theme’s a string, then that will be an error which seems very strange but with Python an inputThe solution is you can change the types in Python and here’s an example where The user enters a number into X and X is a string so we need to change X into an integer so we can use the code x equals into X with the X in brackets and That will change the X from a string to an integer Okay the second part We’ll look I actually using Python

So you looked a little bit coding when you write code so for example to print? Hello world code, and you actually run that how do you use it? Well, you will have notepad on your computer if you open this and you just type in the code print with hello world in brackets if you save that file and Make sure you call the filename, when you name the file use the ending .py. not .txtTxt the text file and you want a Python file, so it’s very important to you finish with .py So save that file in this example. You can call your program my first program so my first program.py

You can either save it on a desktop, so you know where it is Or if you’re organized you can create a folder or choose a folder and put the file in that folder It’s important to know where the file is To run the program we need the command prompt so if you not sure where it is You can click the start button and put cmd Inside the search box just above the start button and that will bring the command prompt up Now you need to know where your file is If you’ve put your file on a desktop you can actually drag the file Into the command prompt and that will make the command prompt go to where your file is And then you press enter to run that file If you created a folder or put the file inside a folder you need to change your directory in the command prompt where your file is So for example if you’ve created a folder called my Python files in your Documents.

It would change directory Using that CD for change directory and then have where the file is stored So you change directory and once you’re in same directory as the file just type the filename Make sure you have .py and this press Enter The command prompt will know it’s Python with the .py and If your code is correct You should print Hello World If it’s not correct you should get an error message One thing you might think well That’s great for running the program, but it’s quite easy to make an error So rather than use notepad.

There’s something called notepad plus now we’ll cover installing that in another video, but Assuming you’ve got that notepad plus you can open now Normal way, you’d open the program, or with your file you can right click your Python file, and if you click Edit you’ll probably be taken to notepad as a program that will open and edit your file and If you open your file, you’ll see that the code is in color so it’s much easier to see any mistakes and to realize what the syntax is were inside Python But remember when you save the file You have to save it as a .py file and you need to know where you saved it so if you’re going to run Python files I’ve used a desktop or my preference is to use a folder .

if you hve any quires regarding our article please let us know in the comment section

Originally published at https://techguidescode.blogspot.com.

--

--

techguide
0 Followers

in this we will provide you all the technology related things like computer technology , tips and tricks and programming languages