3.1.1 - What are variables

Sharon Kodali

Variables: an abstraction inside a program that can hold a value

A variable can be thought of as a container or a box that holds information that your program can use or modify

Variables help you manage and organize your work with data, making your code more organized, readable, and adaptable.

different data types to store variables in

  • integer used to store numbers that can later be used in mathematical operations ex: age or temperature
  • Text(string) Used to store texts lists and words that can later be referenced ex: Name, phone number, or address
  • Boolean used to store simply of true or false ex: is it raining

Correct way to name variables

incorrect: my highs-coreinthegame or n

  • too long and can make the code messy
  • dashes are not allowed when naming variables
  • spaces are not allowed when naming variables
  • descriptive enough to easily recall what the variable repersents

correct: highscore or numstudents or israining

  • short
  • descriptive
  • easy to distingush type of variable

3.1.1 Hacks

instructions: Determine what would be the the best variable name and data type for the specific prompt

  • storing if someones pet is a dog
  • storing someones birthday
  • storing the amount of money someone is spending
  • storing if it is sunny

Homework: write a greeting using variables in python

Naming Conventions

- Must start with a letter or an underscore
- Cannot start with a number 
- Can only have alphanumeric characters or underscores 
- Are case sensitive 
- Cannot be python keywords such as 'else'
myName = "Tara"
print(myName)
Tara
1Tara = "16"
  Cell In[5], line 1
    1Tara = "16"
    ^
SyntaxError: invalid decimal literal
myAge = 16 
myage = 46

print(myAge)
print(myage)
16
46
else = 22
  Cell In[7], line 1
    else = 22
    ^
SyntaxError: invalid syntax

Primitive v. Collection Data Types

Primitive Data Types

- int 
- string 
- float 
- boolean 

Collection Data Types

- list 
- dictionary 
myInt = 20

myString = "My name is Tara"

myFloat = 77.29

myBool = False 
agesOfMyFriendsList = [16, 20, 15, 12]

print(agesOfMyFriendsList)
[16, 20, 15, 12]
{'Tara': 16, 'Tanisha': 15, 'Dylan': 12}
agesOfMyFriendsDictionary = { "Tara" : 16, "Tanisha" : 15, "Dylan" : 12, "Tara " : 17}


print(agesOfMyFriendsDictionary["Tara"])

print(agesOfMyFriendsDictionary["Tara "])

16
17

Concatenation

‘Joining’ of strings and other types

myFirstName = "Tara"

myLastName = "Sehdave"

print(myFirstName + " " + myLastName)
Tara Sehdave
myFirstInt = 5

mySecondInt = 10 

print(myFirstInt + mySecondInt)
15

Formatting allows us to display values using pre-decided rules

message = "My first name is {0} and my last name is {1}"

print(message.format("Tara", "Sehdave"))

print(message.format("Tanisha", "Patil"))
My first name is Tara and my last name is Sehdave
My first name is Tanisha and my last name is Patil
x = .77 

message = "Show this as a percentage {0:.0%}"

print(message.format(x))

print(message.format(.25))
Show this as a percentage 77%
Show this as a percentage 25%
message = "The binary value of {0} is {0:b} and the binary value of {1} is {1:b}"

print(message.format(7, 6836))
and the binary value of 6836 is 1101010110100

Conversions

import json

myDictionary = {"A": 1, "B": 2}

print(myDictionary) 

myStringDictionary = json.dumps(myDictionary)

print(myStringDictionary)

myDictionaryRecreated = json.loads(myStringDictionary)

print(myDictionaryRecreated)

{'A': 1, 'B': 2}
{"A": 1, "B": 2}
{'A': 1, 'B': 2}

Python Data Types in CB Pseudo Code

Primitive Types

a <– expression

  • myInt <– 18
  • myString <– “My name is Tara”
  • myFloat <– 6.8
  • myBool <– False

Collection Types

  • myList <– [1, 2, 3]
  • No dictionary

Data Type Practice

Question 1: Integer Operation

  1. Declare two integer variables, num1Int and num2Int, and assign them values of your choice.
  2. Calculate the sum of num1Int and num2Int
  3. Print the sum

Question 2: Float Operation

  1. Declare two float variables, float1 and float2, and assign them values of your choice.
  2. Calculate the quotient of ‘float1’ and ‘float2’
  3. Print the quotient

Question 3: Format Manipulation

  1. Write a message that equals a sentence with two placeholder variables
  2. Write two strings with two placeholder variables to insert into the message
  3. Print that message

Answers

# Integer Operations

num1Int = 16

num2Int = 12

print(num1Int + num2Int)
28
# Float Operation

float1 = 1.00

float2 = .75 

print(float1 / float2 )
1.3333333333333333
# Format Manipulation 

message = "{0} is {1} years old"

print(message.format("Tara", "16"))

print (message.format("Sharon", "8"))

print (message.format("Dylan", "80"))
Tara is 16 years old
Sharon is 8 years old
Dylan is 80 years old