PYTHON
  • PYTHON v3
  • Python IDE Setup
  • Python Programming
    • Python for ABS
      • Resources
      • Ch1 - Getting Started
      • Ch2 - Types, Variables and Simple I/O
  • Python For Network Engineers-Kirk Beyers
    • Resources
    • Python Fundamentals
  • Python Inststitute (PCAP)
    • Resources
    • Course Introduction
    • Python Essentials (Part 1)
      • Module 1
        • Fundamentals
        • What is Python
        • Starting your work with Python
      • Module 2
        • The Print Function
          • Section Summary
        • Literals
          • Section Summary
        • Operations- Data Manipulation Tools
          • Section Summary
        • Variables
          • Leaving Comments in Code
          • The input () Function
  • 100 Days Of Code
    • Resources
    • What You Have To Do !!
    • 100 DAY's
      • DAY 1: Working with Variables
      • Day 2: Data Types & String Manipulation
      • Day 3: Control Flow and Operators
      • Day 4: Randomisation and Lists
      • Day 5: For Loops, Range & Code Blocks
      • Day 6: Python Functions and Karel
      • Day 7: Hangman
      • SUMMARY
  • {Pirple}
    • Resources
Powered by GitBook
On this page
  • What are variables
  • Variable Name
  • Keywords
  • Creating Variables
  • Using variables
  • LAB 1
  • Shortcut Operators
  • LAB 2
  • LAB 3

Was this helpful?

  1. Python Inststitute (PCAP)
  2. Python Essentials (Part 1)
  3. Module 2

Variables

PreviousSection SummaryNextLeaving Comments in Code

Last updated 4 years ago

Was this helpful?

What are variables

Every Python variable has the following:

  • a name

  • a value (the content of the container

Variable Name

To give a variable a name some strict rules:

  • The name must be composed of upper or lower case letters, digits and the underscore character

  • The name MUST begin with a letter

  • The underscore character IS a letter

  • Upper and Lower case letters are not the same - Alice does NOT equal ALICE

  • The name of the variable cannot be any of Python's keywords

The PEP 8 -- recommends the following naming convention for variables and functions in Python

  • variable names should be in lowercase, with words separated by underscores (eg var, my_variable)

  • function names follow the same convention as as variable names (eg, fun, my_function)

  • Its also possible to use mix case (eg myVariable) but ony in context where thats already the prevailing style.

Keywords

False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield

Creating Variables

A variable comes into existence as a result of assigning a value to it. Unlike in other languages, you don't need to declare it in any special way.

If you assign any value to a nonexistent variable, the variable will be automatically created. You don't need to do anything else.

The creation (or otherwise - its syntax) is extremely simple: just use the name of the desired variable, then the equal sign (=) and the value you want to put into the variable.

What can you put inside a variable? Anything.

Using variables

A variable allows you to store a value by assigning it to a name, which can be used to refer to the value later in the program. For example, in game development, you would use a variable to store the points of the player. To assign a variable, use one equals sign. user = "James" You can use variables to perform corresponding operations, just as you did with numbers and strings:x = 7 print(x) print(x + 3) print(x)

LAB 1

Here is a short story:

Once upon a time in Appleland, John had three apples, Mary had five apples, and Adam had six apples. They were all very happy and lived for a long time. End of story.

Your task is to:

  • create the variables: john, mary, and adam;

  • assign values to the variables. The values must be equal to the numbers of fruit possessed by John, Mary, and Adam respectively;

  • having stored the numbers in the variables, print the variables on one line, and separate each of them with a comma;

  • now create a new variable named totalApples equal to addition of the three former variables.

  • print the value stored in totalApples to the console;

  • experiment with your code: create new variables, assign different values to them, and perform various arithmetic operations on them (e.g., +, -, *, /, //, etc.). Try to print a string and an integer together on one line, e.g., "Total number of apples:" and totalApples.

john = 3 mary = 5 adam=6 totalApples=john+mary+adam print (john, mary, adam) 3 5 6 print(totalApples) 14

Shortcut Operators

If we use the same variable both to the right and left of the = operator Python uses a shortcut for example:

x = x + 2 We can write it as: x+=2

i = i +2*j i+=2*j

var = var/2 var /=2

rem = rem % 2 rem % = 2

j = j - (i + var + rem) j - = (i + var + rem)

x = x**2 x** = 2

a = 6 b = 3 a/=2*b ------------- a = 2/a * 3 1.0 ---- Answer

LAB 2

kilometers = 12.25
miles = 7.38

miles_to_kilometers = ###
kilometers_to_miles = ###

print(miles, "miles is", round(miles_to_kilometers, 2), "kilometers")
print(kilometers, "kilometers is", round(kilometers_to_miles, 2), "miles")
clicks = 12.25
miles = 7.38

miles_to_clicks = (miles * 8)/5
clicks_to_miles = (clicks * 5)/8

print(miles, "miles is", round(miles_to_clicks, 3), "kilometers")
print(clicks, "kilometers is", round(clicks_to_miles, 2), "miles")

LAB 3

x =  # hardcode your test data here
x = float(x)
# write your code here
print("y =", y)
x =  -1   -------------------> change input from 0, 1 and -1
x = float(x)
y = (3*(x**3)) - (2*(x**2)) + (3*x) -1
print("y =", y)

y = -1.0
y = 3.0
y = -9.0
Style Guide For Python Code