0% found this document useful (0 votes)
49 views3 pages

Lecture 1

This document provides an introduction to Python programming concepts like variables, data types, operations, and user input/output through a series of code examples and exercises. The lecture covers basic syntax, math operations, strings, variables, taking user input, and type casting in Python.

Uploaded by

Livs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views3 pages

Lecture 1

This document provides an introduction to Python programming concepts like variables, data types, operations, and user input/output through a series of code examples and exercises. The lecture covers basic syntax, math operations, strings, variables, taking user input, and type casting in Python.

Uploaded by

Livs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lecture 1 Code the Universe

1 Python: Lecture 1 on 16 May 2020


• 5 Minutes - Welcome to Programming! What is Python good for? Why is it so powerful?
• 10 mintues - Introduction To Repl.It
• Remaining - Go through code concepts, examples, etc.

2 Code
2.1 Getting Started

Listing 1: Using print statements


print ( ” h e l l o , world ” )
print ( ” h e l l o , ” , ” world ” )
print ( ” h e l l o ” , ” world ” , s e p = ” , ” )
print ( ” h e l l o , ” + ” world ” )

2.2 Operations
First up, we learn how to use basic math operations using integers.

Listing 2: Basic math operations


print ( 3 + 3 ) # s h o u l d be 6
print ( 3 − 3 ) # s h o u l d be 0
print ( 3 ∗ 3 ) # s h o u l d be 9
print ( 3 ∗∗ 3 ) # s h o u l d be 27
print ( 3 / 3 ) # s h o u l d be 1

However, we can also use math operations with decimal values.

Listing 3: Decimal-based math operations


print ( 3 . 2 + 3 . 3 )
print ( 1 0 . 9 − 9 . 2 )
print ( 3 . 6 ∗ 2 . 5 )

We can also force integer division.

Listing 4: Forcing integer division


print ( 7 / 3 ) # p r i n t s o u t 2 . 3 3 3 3
print ( 7 / / 3 ) # p r i n t s o u t 2 , why?

Finally, we can also use the modulus to find the remainder of a number.

Listing 5: Forcing integer division


print ( 1 0 % 3 ) # s h o u l d be 1 , b u t why?
print ( 2 % 2 ) # i f t h e number i s even , mod w i l l r e s u l t 0 , i f not , w i l l result 1

2.2.1 Task - Distance Formula (Pythag)


Assuming the points (5, 3) and (4, 5) find the distance between the points using the pythag theorem.

Listing 6: Solution
print ( ((5 −3)∗∗2 + (4 −5)∗∗2) ∗∗ 1 / 2 ) ) # n o t e t h e (∗∗ 1/2) i s s q u a r e r o o t .

2.3 Variables
Variables are extremely powerful in Python. Being able to know how to use variables is extremely important.
The following shows you how to define them.

Listing 7: Basic Variables


first name = ” Bill ”
l a s t n a m e = ” Gates ”
age = 64
number pi = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3

print ( f i r s t n a m e + l a s t n a m e )
print ( l a s t n a m e , f i r s t n a m e )
print ( l a s t n a m e , f i r s t n a m e , s e p = ” , ” )

Page 1
Python Lecture 1 Code the Universe

On top of that, you can define multiple variables in the same line and use them for the samme math operations
like above such as addition. However, remember that adding strings is different from adding numbers, as
seen below.

Listing 8: More Variables


n1 , n2 = ( 3 , 4 )
print ( n1 + n2 ) # p r i n t s 7
# OR
n3 = n1 + n2
print ( n3 )

n1 , n2 = ” 3 ” , ” 4 ”
print ( n1+n2 ) # p r i n t s 34

Finally, you can also make multiline strings by using 3 quotes in a row for the beginning and end of a
string.

Listing 9: Long Strings


l o n g s t r i n g = ”””The q u i c k brown
f o x jumped o v e r
the lazy old
dog .
”””
print ( l o n g s t r i n g ) # w i l l p r i n t t h e e n t i r e s t r i n g o u t

2.3.1 Task: Distance Formula w/ Variables


Implement the same distance formula from before except use variables to define the points and define your
solution afterwards.

Listing 10: Problem


x1 , y1 = 5 , 3
x2 , y2 = 4 , 5

# Solution
print ( ( ( x1−x2 ) ) ∗ ∗ 2 + ( y1−y2 ) ∗ ∗ 2 ) ∗ ∗ 1 / 2 )

2.3.2 Task: Say Hi to Yourself !


Make two variables to make strings that take in your name, you can customize this to be any name that you
would like. Once you do that, make it say “Hi” by using a print statement.

Listing 11: Solution


f i r s t n a m e = ” J e f f ” # e n t e r your name
l a s t n a m e = ” Bezos ” # e n t e r your l a s t name

print ( ”Hey t h e r e ” , f i r s t n a m e , l a s t n a m e ) # what w i l l this print ?

2.4 Inputs
One of the most fundamental parts of any language is interacting with the user. In this section, we will
explore taking input from a user and using it within our code.

Listing 12: Taking Input from user


f i r s t n a m e = input ( ” P l e a s e e n t e r your f i r s t name : ” )
l a s t n a m e = input ( ” P l e a s e e n t e r your l a s t name : ” )
age = input ( ” P l e a s e i n p u t your age : ” )

print ( ”Hey t h e r e ” , f i r s t n a m e , l a s t n a m e )
print ( ”You a r e ” , age , ” y e a r s o l d ” )

2.4.1 Task: Simple Email Creator


Use an input statement to collect the first and last names of the person running your program, with this,
use string concatenation (adding strings) to print out a gmail email formatted as: [email protected] if
first name was Bill and last name was Gates.

Listing 13: Solution


f i r s t n a m e = input ( ” Enter your f i r s t name” )
l a s t n a m e = input ( ” Enter your l a s t name” )
print ( f i r s t n a m e+l a s t n a m e+” @gmail . com” )

Page 2
Python Lecture 1 Code the Universe

2.5 Complex Inputs


Before we get into anything, let’s run an experiment based on taking input from the user.

Listing 14: Experimental Inputs


n1 = input ( ” P l e a s e e n t e r a number : ” )
n2 = input ( ” P l e a s e e n t e r a n o t h e r number : ” )

print ( n1 − n2 ) # why do you t h i n k t h i s fails?

This fails with an error, can you think of a reason why? It’s because when we input anything using the
input function, it will be read as a string and you cannot subtract strings. We can prove this by using the
following code:

Listing 15: Built-in Type Function


#To f i n d o u t : t r y t h e b u i l t i n command c a l l e d t y p e ( ) .
#This command a l l o w s us t o f i n d t h e t y p e o f b o t h n1 and n2 .

print ( type ( n1 ) )
print ( type ( n2 ) )

#N o t i c e t h a t b o t h o f t h e s e a r e s t r i n g s .
#This means t h a t Python i s a c t u a l l y i n t e r p r e t i n g t h e c o m p u t a t i o n as ”3” − ”4”
#Or i n o t h e r words l i k e ” J e f f ” − ” Bezos ”

In order to solve this, we can use type casting which is where we force a string that looks like a number to
act like a number. This is done with the following code:

Listing 16: Parsing Strings as Integers


n1 = i n t ( input ( ” P l e a s e e n t e r a number : ” ) ) # i n t ( ) f o r c e s i t t o be i n t e r p r e t e d as an i n t e g e r
n2 = i n t ( input ( ” P l e a s e e n t e r a n o t h e r number : ” ) )
print ( n1 − n2 )

2.5.1 Task: Take an Age and Add 4


Ask the user for their age and add 4 to it, print this back to them

Listing 17: Solution


age = input ( ”What i s your age ? ” )
f i x e d a g e = i n t ( age )
print ( ” I n 4 y e a r s you w i l l be ”+ s t r ( f i x e d a g e +4))

Page 3

You might also like