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

Basic Python

This document provides an introduction to basic Python concepts like print statements, comments, variables, data types, if/else statements, and user input. It explains how to get user input with input(), store values in variables, check variable data types with type(), and use conditional logic with if/else statements. The document encourages readers to practice these concepts by creating a text-based choose your own adventure game related to their school.

Uploaded by

api-328824013
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Basic Python

This document provides an introduction to basic Python concepts like print statements, comments, variables, data types, if/else statements, and user input. It explains how to get user input with input(), store values in variables, check variable data types with type(), and use conditional logic with if/else statements. The document encourages readers to practice these concepts by creating a text-based choose your own adventure game related to their school.

Uploaded by

api-328824013
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

BASIC PYTHON

By Emeka Echezona, Daniel Goodman, and


Scott Wang
what is coding?
IDLE

running
code

writing
code
Python 2 vs. Python 3

Python 2 Python 3

older newer

print Hello World! print(Hello World!)

3/2=1 3 / 2 = 1.5
3.0 / 2.0 = 1.5 3 // 2 = 1

http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
Printing

print(What is your name)


print() #chinese

print(1)
print(2 + 2)

print(I am, 5, years old)

print(I am, 5, years old\n\n)

print()
Comments
#theyarenthashtags
Comments can be found in the program, and are ignored by the computer when it is run
They are generally written to explain how different parts of a program works
This is especially useful when working on collaborative works or even when youre working on
your own and need to take note of something
By using the pound symbol (#) you can create a single line comment
You can write a multiline comment by nesting it between a pair of three double quotation
marks ( )
Comments at work

print(9 ** 3) # In python two asterisks means were using exponents, so the answer is 729
print(8 + 3)
print(YOLO)
print(asdf)
# only two things will be printed: 729 and asdf
Input

input(What is your name)


input(What is your quest)
input(What is your favorite color)

Input
accepts data from the command line from the user
delays entire program until you hit return (or it detects a newline character \n)
returns the data

But how can you make this useful? How can you
grab that data and store it for later use?
Variables

WITH A VARIABLE!
But what is that?
Just like in math, a variable is a symbol (x, y, z, etc.) that stores a value (1, Hello, 3.14, True, etc.)
Unlike numbers and strings of text, variables arent limited to being a single value
They can change as your program progresses
Which is useful for many reasons
BUT WAIT!
Variables arent limited to being a single character!
A variable can be called: soup, dragon, first_name, phone_number_of_donald_trump, etc.
Here is an example of how you can use variables...

first_name = Daniel
C a s e
age = 17
ca mel
print(first_name, is, age, years old.)

BUT HOW DO YOU KNOW WHAT KIND OF THING YOUR VARIABLE IS?!
Data Types
BY LOOKING AT ITS DATA TYPE
By calling the type() function, and using a variable as one of the arguments, you can see its data type
There are quite a few data types in Python. Here are a few:
Booleans (True or False)
Numbers
Numbers can be integers (5, 3, 2), floats (1.34, 33.99999), or complex (3 + 5j)
You can convert a string that contains numerical characters only into a number using
the int() or float() functions
Strings (Hello world)
You can convert many other data types to strings with str()
Lists, dictionaries
Covered more later

x, y, z = False, 36, 40
a = int(y)
print(type(x), type(y), type(z), type(a))
>>> <class 'bool'> <class 'str'> <class 'int'><class 'int'>
If Statements

These let you run some code only if a condition evaluates to True
Their anatomy:

if conditional:
run code

The underlined words must be replaced


The conditional is a snippet of code that must evaluate to True
Things that evaluate to True:
5 == 5
True
5 >= 3.14
If Trees

x = int(input(Enter a number: ))
if x < 5:
print(Bad job)
elif x == 5:
print(Nice job!)
else: # Doesnt even need a conditional! Used as a catch-all.
print(Worst job)
Nested If Trees
why did we do this?

age = int(input(Enter your age: ))


finishedMiddle = input(Have you finished middle school? (y/n): )
if age < 14:
if finishedMiddle == y:
print(Liar!)
else:
print(Cool!)
elif age >= 14:
if finishedMiddle == n:
print(Liar!)
else:
print(Cool!)
Your Turn!

Make a Choose Your Own Adventure Game


The game must
Be HTHS related
Be school appropriate
Use concepts discussed today (print, input, variables, and if statements)

r O w n
e You
Choos

Adventure

You might also like