0% found this document useful (0 votes)
84 views

CODE1

This document introduces Python programming concepts like variables, conditionals, loops, and functions through a series of coding exercises. It demonstrates how to write, run, and modify simple Python scripts on IBM Z systems. Key concepts covered include variables, if/else statements, loops, functions, and basic Python syntax.

Uploaded by

Tom gu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

CODE1

This document introduces Python programming concepts like variables, conditionals, loops, and functions through a series of coding exercises. It demonstrates how to write, run, and modify simple Python scripts on IBM Z systems. Key concepts covered include variables, if/else statements, loops, functions, and basic Python syntax.

Uploaded by

Tom gu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Fundamentals 230422-1718 

   

CODE1
Make Your Own Fun
• PYTHON IS A GREAT LANGUAGE TO HAVE ON Z
• 1 IF YOU WANT TO CODE, THEN …
• 2 LET’S SEE WHAT’S INSIDE
• 3 A WORD ON EXTENSIONS
• 4 A ROSE BY ANY OTHER NAME
• 5 A BIT OF AN ANTICLIMAX ?
• 6 BLOCKS FOR THE BETTER
• 7 OR ELSE!!
• 8 DON’T LOSE YOUR MARBLES
• 9 THE FINAL COUNTDOWN
• 10 I’M A VISUAL LEARNER
• 11 ALL OUT OF MARBLES
• 12 DOUBLE-CHECK; SUBMIT

IBM Z Xplore
PYTHON IS A GREAT LANGUAGE TO HAVE ON Z
The Challenge

You’ll be introduced to the basic concepts of coding here. Or, if you’re already familiar with
variables, loops, and ‘if’ statements, you’ll be quickly getting a refresher and learning that it
doesn’t matter what platform you’re on, Python code looks like Python code.

You’ll be using the Python programming language, as it’s perhaps the most widely-used language for new
development right now, and it makes it very easy to get started with simple code that won’t overwhelm
you if this is your first time coding.

Before You Begin

CODE1|230422-1718
All you need to get started is access to the USS shell, either through an SSH program, or a terminal
window.

The only technical challenge you need to have completed before this one is VSC1.

Investment

Steps Duration

12 120 minutes

IBM Z Xplore Copyright IBM 2021-2023 [2/20]


1 IF YOU WANT TO CODE, THEN …

CODE1|230422-1718
SSH into the IBM Z system and check your home directory for a file called “code1.py” (using the ls
command); if you do not have one, copy the original code1.py program from /z/public, either by copy-
and-pasting through VSCode, or by entering the following command:

cp /z/public/code1.py ~

(as shown in the above screenshot)

Next, run the program with

python3 code1.py

This says “Use the Python interpreter to run the code1.py program in this directory” and look at the
output.

As you can see, there are lot of things happening in here; in the next step, you will take a look
“under the hood” and see what’s going on inside.

IBM Z Xplore Copyright IBM 2021-2023 [3/20]


2 LET’S SEE WHAT’S INSIDE
Open up the code in an editor window within VS Code.

There is a small amount of code to look at in here, and lots of comments (those are the lines in green
starting with #)

CODE1|230422-1718
Even if you have never programmed before, you can see the part where it

• counts backwards from 5


• reads your userid
• figures out what time it is
• and how it stops for a second between each section.

You may find some of this information helpful as you tackle later steps. (that’s a hint, right there!)

IBM Z Xplore Copyright IBM 2021-2023 [4/20]


3 A WORD ON EXTENSIONS
Extensions in VSCode are a nice way to gain additional functions, but they can also make things not
work as expected.

You might get a message from VSCode asking if you want to install the Python extension.

CODE1|230422-1718
If you do have a Python extension installed, you can try to keep using it, but be aware you will
probably get warning messages in later Python-related challenges.

One to look out for in particular is an error messge telling you that the ‘zoautil_py’ module cannot be
found – this is NOT a problem for you working on these challenges.

If you want your editor to look like the screenshots here, don’t install any extensions besides the
ones outlined in the challenges.

All good? Let’s move on …

IBM Z Xplore Copyright IBM 2021-2023 [5/20]


4 A ROSE BY ANY OTHER NAME
Make sure you have a copy of over code2.py in your home directory.

CODE1|230422-1718
This Python program takes a word and figures out if the letter “z” is in it.

Notice how ‘the_word’ to set to “pumpkin” on line 7.

This is creating what’s known as a variable.

A variable is a placeholder for a value in a program, and it can be letters, numbers, or really any
sort of data.

In this case, it is used to represent the word you want to “investigate” - to check if it contains a
“z”.

The program also has a variable for holding the value of the particular letter you use for the
investigation.

IBM Z Xplore Copyright IBM 2021-2023 [6/20]


Read the program to understand what it should do; then run the program and see what happens (use
python3 code2.py ).

CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [7/20]
5 A BIT OF AN ANTICLIMAX ?
Nothing happens, right?

Let’s fix that!

CODE1|230422-1718
Edit the code so instead of “pumpkin” the variable word is set to “pizza”, save the file, and run the
program again in your USS terminal session.

You should see a message confirming that “pizza” does in fact have the letter “z” in it.

Feel free to experiment with other words, and letters, and then look at the part of the code that does
the checking.

Note: Make sure to save your file between edits (by going using the editor menu option File -> Save, or
using the keyboard shortcut) to commit any changes you make, and remember to the run the code in your
USS terminal session.

IBM Z Xplore Copyright IBM 2021-2023 [8/20]


6 BLOCKS FOR THE BETTER
A big part of writing and understanding code is figuring out the logic of a program.

In other words, the way it flows.

Program code can generally be organized into blocks of code that can be run in order, or if a condition
is met.

CODE1|230422-1718
This program uses an ‘if’ statement on line 13.

It is testing to see if ‘the_letter’ is in ‘the_word’.

An ‘if’ statement has the condition being tested, followed by a colon ( : ) and the code which will be
run, when that condition is satisfied, is in a “block” underneath it.

A block is a group of program statements that all belong together.

The block of code is “indented” (moved to the right), so you know that anything in that block is what’s
going to happen if the condition is met.

IBM Z Xplore Copyright IBM 2021-2023 [9/20]


WHY DO WE NEED MORE THAN ONE LANGUAGE? CAN’T I JUST LEARN ONE?

Different languages are better at different things.

COBOL is a language built for high-precision and low-latency transactions.

C and C++ are good for system utilities and high-performance applications that benefit from lots of
control over memory and other system resources.

Python is an extensible language that has been around for a long time and used for a lot of
different tasks. You can call it a General-Purpose Programming Language, as you can use it for

• AI (Artificial Intelligence)
• mathematical simulations
• web applications

CODE1|230422-1718
• games
• automation
• just about anything.

If you’re going to learn just one programming language, it makes sense for it to be Python.

From there, once you have the fundamentals understood, it’s just a matter of learning any other
programming language’s particular points.

Code on!

IBM Z Xplore Copyright IBM 2021-2023 [10/20]


7 OR ELSE!!

CODE1|230422-1718
Lines 15 and 16 in code2.py add another step to the ‘if’ statement - an ‘else’ clause.

If the condition being tested on line 13 is not met (there is no ‘z’ in the word) the program can say
or do something about that.

Right now, these lines of code are commented out, so they do not actually do anything.

Delete the hash marks ( # ) at the beginning of the lines in the ‘else’ block, save the file, and then
run it again.

Make note of the indentation - the ‘else’ should line up with the ‘if’, and the next line (or block or
lines) should be indented to show that it belongs to the ‘else’ above it.

Test it out and make sure it works for ‘z’ and non-‘z’ words.

IBM Z Xplore Copyright IBM 2021-2023 [11/20]


8 DON’T LOSE YOUR MARBLES
In your USS terminal session, try to run the ‘marbles.py’ program from your home directory.

Hopefully, this should be pretty straightforward.

Open up the file in your editor to check out the code.

CODE1|230422-1718
An important thing to notice is how the message about how many marbles are left appears in the code
only once but gets printed out 10 times.

This is because we have used a ‘while loop’ starting at line 6.


A ‘while loop’ says, “Keep doing this set of actions as long as the following condition is true”.

In this case, the condition is true as long as there are more than 0 marbles left.

IBM Z Xplore Copyright IBM 2021-2023 [12/20]


9 THE FINAL COUNTDOWN
Once you have traced your way through the ‘marbles.py’ code, take a look at lines 13and 14.

That looks like a special feature that you can enable by uncommenting.

Erase the comment marks so the code is active and run the program again.

The code was supposed to issue a warning message when there are three or fewer marbles left, but
something is wrong with the logic in that ‘if’ statement.

OH NO!!!

CODE1|230422-1718
See if you can fix it so the message prints out just like the screenshot above - only when there are 3
or fewer marbles left.

A few hints to get you started:

• >= means “greater than or equal to”


• < means “less than”

IBM Z Xplore Copyright IBM 2021-2023 [13/20]


Consider where this ‘if’ statement is in relation to the part of the code that subtracts one marble.

Hmmm…

There are many different ways you can fix this code.

CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [14/20]
“HOW ELSE CAN I CONTROL CODE?”

We’re really just skimming the surface of Python programming in this challenge, enough to give
someone with no programming experience a chance to check out what coding looks like with lots of
examples.

If you are keen and want to learn more, be sure to read more about what you can do with Python at
IBM Developer .

There are videos, tutorials, podcasts, code patterns, and lots of projects you can get started on,
no matter what your current skill level is.

Even if you don’t plan to be a fulltime coder, it’s important to have some familiarity with code so
you can fix problems when they come up, and maybe even add features to code someone else wrote.

CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [15/20]
10 I’M A VISUAL LEARNER
Numbers are nice, but sometimes it is better to visualize things.

Your next task is to make the program print out the number of marbles left as asterisks. So when
there’s 5 marbles left, you print *****

How are you going to get this done?

One of the variables in the program ‘marble_dots’ is just ten asterisks in a row.

Maybe you were curious what that was for.

The following line of code will make the program print out six marble dots.

CODE1|230422-1718
print(marble_dots[:6])

If you replace the ‘6’ with a variable name, it will print out the number of asterisks for whatever the
variable is set to. (Assuming the variable has been set to a number!)

Are the wheels of your mind turning yet?

IBM Z Xplore Copyright IBM 2021-2023 [16/20]


Figure out where you want to put that line of code and how to make it work so that the result looks
like the output in the screenshot above.

CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [17/20]
11 ALL OUT OF MARBLES

CODE1|230422-1718
The user needs to know when the program is done, and that there are no more marbles left.

Make the message say “You are all out of marbles”, make sure it appears last, and only once in your
program.

IBM Z Xplore Copyright IBM 2021-2023 [18/20]


12 DOUBLE-CHECK; SUBMIT
Now it’s time to run the CHKCODE job from ZXP.PUBLIC.JCL and see if you have “crossed all of your T’s
and dotted all of your lowercase I’s”.

Make sure you have the right number of “marbles” displayed, that the spacing looks right, and that your
final “all out of marbles” message looks just like it does in the screenshot.

CODE1|230422-1718
The validation job will run your ‘marbles.py’ program, so make sure it’s all working as described.

This is just the beginning; you’ll do even more with Python in later challenges, so hopefully you’ve
enjoyed this.

IBM Z Xplore Copyright IBM 2021-2023 [19/20]


Nice job - let’s recap Next up …

In most things, the hardest part is getting started. If this was yourfirst time
hacking away at code, it might have felt difficult,frustrating, and even a little
weird.

We picked an example that would illustrate a few of the most important concepts: + Wrapping up
variables + loops + if/then/else statements + modifying variables + using library Fundamentals
functions.

Completing this challenge means you’re able to envision a problem, mentally solve
it, and then implement the solution in code.

CODE1|230422-1718
IBM Z Xplore Copyright IBM 2021-2023 [20/20]

You might also like