#!/usr/bin/env python # coding: utf-8 # In[25]: # Section 2.4 - Comments # Comments are used to make code more readable! # # HTML: # # # 2.5 Variables x=2 #print(y) <-- makes an error bc y hasn't been defined yet print('x') #print(X) <-- this makes an error bc python thinks of x and X as different # # Example # governor='Jim Justice' secofstate='Mac Warner' print('The governor is currently') print(governor) print('The secretary of state is currently') print(secofstate) # 'Governor'=gov <-- this is an error because it is backwards # Rules for variables # 1. can't use a "keyword" of python <-- see Table 1-2 in Section 1.4 #example to avoid: if='Jim Justice' # 2. can't have spaces in variable names # example to avoid: sec of state='Mac Warner' # 3. first character of a variable must be a letter A-Z or a-z OR an underscore _pies='apple' # example to avoid: 76pies='5' # 4. after first character, can use numbers, letters A-Z, letters a-z, and underscores president1='George Washington' # 5. Variables in python are case-sensitive (e.g. Age and age are different) # ADVICE: name your variables for what they are used for # better to do # pizzassold=10 # instead of # p=10 # # # # Multiple items with a single print # earlier with governor example -- had multiple lines from the prints # now we can avoid it: print('The governor is',governor) print('The secretary of state is',secofstate) # # # Variables can change value # ################ # Assign a variable dollars=1.53 print('I have',dollars,'in my account!') dollars=35.23 print('I have',dollars,'in my account!') # the dollars variable is a "float" pi=3.14151926 #also a float -- because it has a decimal point zloty=5 #NOT a float -- it is an int lei=5.0 #NOT an int -- it is a float # WE can check the variable type using the type function print('dollar type is:',type(dollars)) print('zloty type is:',type(zloty)) print('lei type is:',type(lei)) print('governor type is:',type(governor)) zloty=5.0 print('zloty type is now:',type(zloty)) # # Moral for numbers: a float has a decimal point, an int does not! # # # In[2]: # Section 2.6 - Keyboard input # # #Example ###### # Get a user's name #name=input('What is your name?') #print('Your name is',name) # Get first and last name: firstname=input('What is your first name?') lastname=input('What is your last name?') print('Hello',firstname,lastname,'have a good day!') #fact: input when stored as a variable is ALWAYS a string print(type(firstname)) #### # # In[5]: ### Example of typecasting to int # # #get name, age, and income of user name=input('What is your name?') age=int(input('What is your age?')) income=input('What is your income?') print(type(age))