0% found this document useful (0 votes)
6 views

Python Tutorial

The document introduces basic concepts of Python programming, including comments, variables, conditions, loops, and functions. It provides examples of how to use print statements, create variables, implement if-else conditions, and utilize loops for repeated actions. Additionally, it explains how to define and call functions with arguments to perform specific tasks.

Uploaded by

eshaansbusiness
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Tutorial

The document introduces basic concepts of Python programming, including comments, variables, conditions, loops, and functions. It provides examples of how to use print statements, create variables, implement if-else conditions, and utilize loops for repeated actions. Additionally, it explains how to define and call functions with arguments to perform specific tasks.

Uploaded by

eshaansbusiness
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 10

Getting to know Python

If you have Python, you installed an app called


IDLE. The shell window opens when you first open
IDLE. It starts with a “>>>” at the beginning.

>>> # type here

Comments
Remember the “#” in the first paragraph? The #
means its a comment. They are ignored by the
computer.
Type “# print(“Hello World”)” then Enter (or
return).

>>> # print(“Hello World”)


>>>

See? Nothing happened! Now type it without the #:

>>> print(“Hello World”)


Hello World
>>>

This time, there was no #, so the computer ran the


program. print() is a function that writes the text
down and shows it.

Variables
Variables are things that give a name to a thing. To
create one, type the following:

>>> vrblName = “vrblValue”


>>>

Variables can contain boolean expressions,


integers, strings, floats, inputs, lists, or “answers”.

Boolean Expression = True or False


Integer = Number with no decimal, 0, 5, 64, -5, -
1000
String = List of characters, like “Hello World”
“Worldy World”
Float = Number with decimal, 1.2, 3.4, 0.0, 6.9, 7.8
Input = A question, input(“What’s your fav
color? ”), then it saves the value
Answer = +, -, *, ÷, 4+2, 5*6, 15÷3, 6-3
list = A list, [1,2,3], [1, “2”, 3.0, [1,2,3], 4]

You can also print variables:

myVariable = 1.2
>>> print(myVariable)
1.2

Conditions
Maybe in Python you want to ask something like
“Do I have enough memory for my calendar?” or
“Is my answer correct?” You actually can! You just
use if! Example:

if this happens:
then do this

Another example:

>>> myWord = “Hello”


>>> if myWord == “Hello”:
print(“Found Hello”)

Found Hello

== — Equal To
!= — Not Equal To
> — Greater Than
< — Less Than
>= — Greater Than or Equal To
<= — Less Than or Equal To

Try it yourself.
We can use it in a program:

answer = input(“4+5: ”)
if answer == “9”:
print(“Correct Answer!”)

It will ask you: 4+5: 9


I entered 9, and it said Correct Answer!
If you enter anything else, it won’t say anything.
We’re going to talk about that in the next part.

Elif and Else


Pretend you have an if condition, and nothing
happens when the condition is something else. To
change that, put in an else condition. Example:

if this happens:
do this
if something else happens (else):
then do this

Another example (from the previous chapter):

answer = input(“4+5: ”)
if answer == “9”:
print(“Correct Answer”)
else:
print(“Oops... Wrong Answer”)

Now, if I enter 9, it says Correct Answer, but if I


enter anything else, it says Oops... Wrong
Answer. Hooray! But, if I enter something wrong, it
should keep on asking. We’ll see that in the next
chapter.

Pretend you want to use else and if. Instead use


elif in this format:
if I am happy:
say you are happy
elif I am not happy:
say I am not happy
else:
say you must be something else

Let’s use it in a program:

answer = input(“4+5: ”)
if answer == “9”:
print(“Correct Answer!”)
elif “9” in answer:
print(“Close, but still wrong.”)
else:
print(“Wrong”)

Now, if I enter 9, it says Correct Answer!, but if I


enter a number with 9 in it, like 95, it says Close,
but still wrong, and if I enter something else, it
says Wrong.

Loop the Loop


If you keep on doing something again and again
and again and again in Python, you should use a
for loop. Example:

for counterVariable in
range(howManyTimesRepeat):
print(“Hey”)

Here’s an actual example:

for looper in range(5):


print(“Hello”)

If we run this, we get:

Hello
Hello
Hello
Hello
Hello

Can you understand how this works? First, we tell


it’s a for loop (for) then we tell the counter variable
(looper) then we tell it’s looping (in) then we tell
how many times. (range(5)) So, it prints “Hello” 5
times. Here’s a different format:

for i in [1,2,3,4,5]:
print(“Hello”)

Can you guess the difference? Instead of range, we


used [1,2,3,4,5]. So why use range and not use
[1,2,3,4,5]? Well, range is a shortcut because what
if you want to loop it 100 or 1,000 times? Well, that
would be a lot of typing!
for i in
[1,2,3,4,5,6,7,8,9,10,11,12,13...]:
print(“Hello”)

There’s also a different type of loop: A while loop.


It’s used when you want to make a loop with a
condition. It’s used when you don’t know how
many times you need to repeat. Example:

while this_condition_is_true:
print(“condition is true”)

And here’s a real example:

i = 9
while i < 13:
import time
time.sleep(1)
i = i + 1

Don’t worry about import time and the other stuff


we haven’t talked about. Just focus on the main
thing:

1. The variable i is assigned 9.


2. We create a loop.
3. The loop runs as long as i is less than 13.
4. In the loop, it waits one second.
5. Then it adds 1 to i.
input() examples
Remember input? We talked about it way back
when we were talking about things variables can
hold (+, -, *, ÷)
Try to make a program:

1. Ask for your name with input


2. Print your name

Answer:

name = input(“What’s your name? ”)


print(“Your name is”, name)

If I enter Fred, I get Your name is Fred.

If you want a string to be inputted, use str(),


integer is int(), float is float().

name = int(input(“Enter 49: ”))


if name == 49:
print(“That’s 49”)
else:
print(“That’s something else”)

Run this. If I enter 49, I get That’s 49, but if I enter


1000000, it says That’s something else.
Functions
Functions are things that do something, like print(),
input(), int(), and more. Here’s how we create a
function:

def functionName():
print(“Stuff here”)

Here’s an actual example:

def greeting():
print(“hey”)

To use the function, you have to call it. This is how


you do it:

greeting()

You will get:

hey

But what are the parentheses () for? They are for


arguments!
A function can do something for you, right? But you
can also give the function something. That’s an
argument. Example:

def printMyName(yourName):
print(yourName)
printMyName(“Johnny Plum”)

If you run this, you get:

Johnny Plum

The yourName argument is like a variable, but you


can only use it in the function. Let’s have a more
advanced function with more arguments:

def myInfo(Your_Address, Your_Number):


print(“Your address is… ”)
print(Your_Address)R
print(“Your phone number is…”)
print(Your_Number)

myInfo(“100 Main Street, San Francisco,


California, USA”, “(123) 456 - 7890”)

This will give you:

Your address is…


100 Main Street, San Francisco, California,
USA
Your phone number is…
(123) 456 - 7890

You might also like