Variables.

Variables/Value Names

Variables in Python

What is a variable in Python?

A name (identifier) for "container" (placed in RAM) in which values can be stored and retrieved
When we say that we set value in a variable, it should be understood as to write a value into the corresponding container.
We can get the variable value, i.e. to read the content of the corresponding container

			x = 99
			y = 3.141516
			first_name = "ada"
			sur_name = "byron"
			num_list = [1,2,3,4,5]
		

Identifiers rules

Variables names should follow next rules:
Start with letter or underscore
Followed by zero or more letters, underscores and digits

				x1 = 5 # ok
				1x = 5 # error
				_  = 5 # ok
				__x__ = 5 # ok
				my-var =5 # error
			
Variable names are case-sensitive

				a = 5
				print(a)	# 5
				print(A)	# NameError: name 'A' is not defined. Did you mean: 'a'?
			
Identifiers and keywords @python docs

Naming Conventions

Naming convention is not a rule, but a generally agreed scheme for naming things.
Naming conventions make programs more understandable by making them easier to read.
Different programming languages uses different naming conventions
Python uses snake_case naming convention for naming variables
variable names should be lowercase, with words separated by underscores. That naming convention is known as snake-case.

				user_name = 'Ada' # snake_case -> used in Python
				userName = 'Ada'  # camelCase -> not used in Python for simple variable names
			

Assignment Operation (Name binding)

Name binding is the association between a name and a value
We can bind a value to a name with the assignment operator (=)
We can check the unique identity of a value by the id() built-in function.

				a = 2
				print(id(a))

				# 140168672297232
			

Name binding

Exercises

Academical to Astronomical hours converter

The task

Given that:
An astronomical hour consists of 60 minutes
An academical hour consists of 45 minutes
For each 3 academical hours there are 15 minutes brake
Write a Python program, that will print out how many astronomical hours a python course should take, if it consists of 64 academical hours, including the respective brakes
Try to think of a solution, which will be readable enough and easy to maintain in future
Write your code in HW/acad_to_astro_hours.py file

Body Mass Index calculator

The task

Calculate your body mass index (BMI) with Python, given the formula:
BMI = W / (H*H)
where:
W = weight_in_kilogram
H = height_in_meters
Your program should outputs the result rounded to 2 digits after the decimal point).
Try to think of a solution, which will be readable enough and easy to maintain in future (you can use comments and semantic variable names)
write your code in HW/BMI.py file

Tip: You can use the python's build-in round() function, like:

				>>> round(2.1457, 2)
				2.15

				>>> round(1.4234,2)
				1.42

				>>> round(1.4284,2)
				1.43
			

HW Submition

You can submit your files either by:
upload to your Github account
Or sending as email attachment