Learning Python – Lesson 3: Introducing Variables

Free Python Training

We all know that variables are a key part of programming and scripting languages, and sure enough, they are a big part of Python. Here is how simple they are to implement:

#Today we are going to create and use some variables! 
#This is really cool! 
#Hey just to remind you, these are comments that are here to make our code "self-documenting"! 
#Python itself is ignoring the s#$t out of these! 
my_books = 14
my_years_on_planet = 47
books_per_year_on_planet = my_books / my_years_on_planet
print ("Below is my books per year on the planet!")
print (books_per_year_on_planet)

Notice here I am using Python 3 so I need parenthesis around the objects and text that I want to print instead of the older quotation marks! This is the most noticeable difference between Python version 2 and version 3. The PRINT function got a nice little update in this regard.

Here is the result of my program!

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
======================= RESTART: C:\Users\terry\ex3.ps =======================
Below is my books per year on the planet!
0.2978723404255319

>>>
If you are you are using Python 2, you need the “older” syntax to make those print functions work right. Here is that version of our code for you:

#Today we are going to create and use some variables! 
#This is really cool! 
#Hey just to remind you, these are comments that are here to make our code "self-documenting"! 
#Python itself is ignoring the s#$t out of these! 
my_books = 14.0
my_years_on_planet = 47.0
books_per_year_on_planet = my_books / my_years_on_planet 
print "Below is my books per year on the planet!" 
print books_per_year_on_planet

Notice another change here – I need to indicate my variables are of the Float type in order to display my result in the format I need. So I enter my 14.0 and 47.0 for the variable values. I will discuss this in greater detail in the next post!

One final note, if you wanted to use the print function so that it would work in both version 2 and version 3, you would want to use both the ( and the ” as I did in the first program above. For example, I am using Python 2 now:

print ("Hello World!")

This returns:

Hello World!

just as it would in Python 3.

Remember, free Python training is waiting for you at CBT Nuggets!

CBT Nuggets’ Windows 10 70-697: Configuring Windows Devices Course Outline

70-697

I know that many of you are going through the Nuggets as they publish each week here at the CBT Nuggets site. Here is the complete course outline so you can easily track your progress. Enjoy! By the way – the Nuggets you see in bold means I have completed and uploaded them. If they do not appear in your course yet – it is because they are going through the very stringent review process.

  • Nugget 1: 70-697 Course Introduction
  • Nugget 2: Key Administration Tools
  • Nugget 3: Install Apps Using Office 365
  • Nugget 4: Windows Store Apps
  • Nugget 5: Sideloading Apps
  • Nugget 6: Using your Microsoft Account with Windows 10
  • Nugget 7: Other Authentication Options
  • Nugget 8: User Profiles
  • Nugget 9: Hyper-V
  • Nugget 10: Offline Files
  • Nugget 11: Windows To Go
  • Nugget 12: WiFi Direct
  • Nugget 13: Power
  • Nugget 14: BitLocker
  • Nugget 15: Manage Devices with Microsoft Intune
  • Nugget 16: Support Mobile Devices with Intune
  • Nugget 17: Deploy Software Updates by Using Microsoft Intune
  • Nugget 18: Configure IP and Network Settings
  • Nugget 19: Configure and Maintain Network Security
  • Nugget 20: Data Storage Topics
  • Nugget 21: EFS
  • Nugget 22: Share and NTF Permissions
  • Nugget 23: Libraries
  • Nugget 24: HomeGroups
  • Nugget 25: Other Printer and File Options
  • Nugget 26: Configure Remote Connections
  • Nugget 27: Deploy and manage Azure RemoteApp
  • Nugget 28: Support desktop apps
  • Nugget 29: Your 70-697 Exam