Python Programming | the Basics
by mrw122015 in Circuits > Electronics
1824 Views, 52 Favorites, 0 Comments
Python Programming | the Basics
Python can be a very complex and hard to understand the programming language, but after this Instructable, and with some practice, you will be well on your way to being a programming geek!
Using "print"
The print command is used to show text to the user from the program. A simple way to use the command is by typing
print "can you see this?"
If you run this program, you will get an output that says
can you see this?
We will use this command later on in a more complex way.
Try it out yourself: Use the following program, but change the text in quotes to see what happens.
print "Try It Yourself"
Downloads
Using Variables
Variables are used to take the place of something else. They will become important later, but it is important that you learn how to use them now. Consider the following code:
text = "hi" print text
The first line assigns something to the variable called text. Then, when you tell the program to print text, it will print what it represents. In this case, it would print out hi. You can make the variable itself anything that you want, except for any command in python. This means you cannot name a variable something like print.
You can also assign numbers to a variable. Just follow the instructions above.
Try It Yourself: Using One Variable and one print command, make your program tell your "your smart!"
Downloads
Your Best Friend | #
# will become your best friend in Python. It is used to make notes and hide line(s) of code when it is running. Use it like this:
print "hi" #This is a note
or, in this case, nothing from this line would run
#print "hi"
Try it yourself: Print a message, but have a note after and make it run without an error.
Downloads
If and Elif | Life Is Not Complete Without Them
Most python programs would not be able to function and have the capabilities they have without if and elif. It is used like this:
myvariable = "dog" #makes myvariable a variable that represents dog if myvariable == "dog": #It is saying "If myvariable is equal to "dog" print "dog is dog" #Then print "dog is dog"" elif myvariable != "dog": #"If myvariable is not equal to "dog" print "Dog is not dog" #Then print "dog is not dog""
You can also use if and elif to solve an equation. (Note: To multiply, us * and / to divide) Python will solve 5-1 and if the answer is 3, it will print "three". If it is not 3, it will print "Not Sure"
x = 5 y = 1 z = 3 myequation = x-y if myequation == z: print "three" elif myequation != z: print "Not Sure"
Try It Yourself: Make Python solve 10+10
Downloads
Using Raw_input
raw_input allows the user to enter information into the program to make it do something.
For example, if you want to print a list of items you need at the store later, you would use something like this:
inputtoprogram = raw_input("What should I say?") name = raw_input ("What is your name?") print name + " said to say: " + inputtoprogram
Try it yourself: Make a program to ask for your full name and age and print it with just one print command and 4 raw_input commands at the most.
Downloads
Some Cool Programs
Here are some really cool programs that I have built using python. Even though there may be some new things in them, you will see that the basic frame of the program using things that you learned above.
The File called gmailsendmail.py is a program that works to send an email through gmail. It works really well and quickly. If it does not work, that means your login credentials are wrong, or your firewall is blocking the connection and you need to speak with your network admin. What is most likely blocked is port 587 on smtp.gmail.com
The File called gmailsignincheckmail.py uses your login credentials to makes sure your message can be sent before you take the time to type it out. It is pretty much the same as the last program.
Please Note: This next program (SaveFileFast.py) only works on the Mac computer. Use the text commented out to help you understand how to use it. *Do not create a file in the same location and same name. This will delete the old one.
Please take the following quick survey to help me improve my instructables! I would really appreciate it! https://goo.gl/forms/g6c88vggPdhmTN393