Understanding Python- TechWeu

Published on:

So welcome back. Now we’re going to start learning about the Python language and you can think of this as talking to Python itself. And it turns out that there is a way on most computers – whether it’s a Windows computer and command line or a Macintosh or Linux box – to get Python started. And if you just run Python, it will take, interactively, commands that you can type. And you have to type “python” at this chevron prompt, is what we call it, but this also sort of fits into what next. So Python is like “Okay. I’m here. I can handle any Python statement you can send me. I’m ready to do whatever it is that you want to do. I don’t know what to do. I need you to tell me what to do.” And so you can type a series of statements in Python

And so the first statement that you might type is an assignment statement; x equals 1. And so what is going on here? Now this, this assignment statement is something that often confuses people when they move from math to programming. An equal sign sort of has a direction to it; it’s an arrow. It really is saying “Dear Python, you’ve got a lot of memory – you’ve got a lot of memory. Take a little tiny piece of that memory and remember it and name it x. I might use that – I’m going to use that later – and stick a 1 in it.” So this is sort of like stick 1 in a spare place in memory and name it x. print (x) says, go take whatever that spare bit of memory was and bring it back out and tell me what I put in it. Now, it’s kind of redundant, but usually you’re doing something more complex than this. Put something in memory and then take it back out; that’s the first thing.

Now what this is doing is this is an expression and that says take whatever’s in x, which is a 1, and then add 1 to it, which becomes 2, and then stick it back in x. So that adds 1 to x and then we print that out and it’s a 2 and then we quit. Now if you type wrong things here, you’re going to get syntax errors. And Python is just going to tell you, you know, syntax error, syntax error, syntax error, and away you go. But this is us talking to Python. So what do we say to Python? You can almost think of this as like writing an essay where you start – and if you think back, you started learning an alphabet and then you used that alphabet to produce words and then used the words to produce sentences and then you combined sentences to make paragraphs, which then make a story. 

This is a Python story about how to count words in a file in Python. Okay? This solves that problem that I asked you to solve a little while back of what the most common word was and how many there are.So if we start at vocabulary, the first thing we have in every programming language is what’s called reserved words. Now, what do we mean by reserved words? Well, these are words that if we use these words, we must use them to mean the thing that Python expects them to mean. Another way to put that is we can’t use them elsewhere. We can’t make up a variable named import. We can’t make a variable named assert; because if Python sees assert, it means something very specific to Python. If it sees if, it means something. If it sees for or pass or while – where is while? There is while. These mean things. 

The best way to think about this is Python is kind of like a dog. And if you’re talking to a dog and you say hey, dog, do you like, you know, Beethoven or Bach better? The dog is hearing – is like blah, blah, blah, blah, blah, blah – it doesn’t care anything. And then you say hey – hey there, Spot. What do you think of the weather today? Do you really like, you know, sunsets or do you like rain better? And the dog is like blah, blah, blah, blah, blah, blah. And then you say something like do you think it’s time to go for a walk? And the dog goes, like, walk – got it. And the dog’s been listening to you all along. Most of it is blah, blah, blah, blah, blah, but walk is a reserved word for a dog. Okay? Food, treat – those are reserved words for dogs. Most of stuff you can say anything you want to a dog and they’re very good listeners, right? But when you say a reserved word for a dog and you say walk and you don’t deliver and then that dog’s gonna bug you for a while. So you can think of that as Python. When Python sees global or from or for, it’s like whoa – that’s a word that means a lot to Python. So you’d better use it the way Python expects you to use it.

So just understand that we learn little bits and little pieces and then it gets better toward the end. So thanks for listening.So I hope you find this value and see in the next post.


Leave a Reply

Please enter your comment!
Please enter your name here

Aksh Swanni
Aksh Swanni
Aksh Swanni writes about Technology, smartphones, & Gaming for Techweu, out of Delhi. Aksh is technology & gadgets Author for techweu, and has frequently written about technology, gadgets on all platforms and new developments in the world of smartphones. In his free time, Aksh likes to spend time on his Play Station, learn new technologies, watch informative videos, and watching movies. Aksh is available on Aksh@techweu.com, you can send in your leads and tips.

Want to be a Tech-Savvy? Sign-Up & Flaunt yourself within your Friends...