Tag Archive - line

Hello Python

As I talked about earlier, I am beginning to learn Python. I thought that this could be a good opportunity to document what I am learning and share my thoughts, experience and code.

To be totally cliche, I figured I would first demonstrate how to create the Hello World program in Python.

The most simplistic method to accomplish this is to simply print out the phrase:

print "Hello World!"

This looks almost exactly like PHP code expect you will notice there is no semicolon at the end of the statement. That is because Python does not use the semicolon to end a line.

Ok, let’s take this to the next level now. Let’s put this string inside a function that takes in a single parameter.

The function code looks like this:

def helloWorld(name):
  return "Hello " + name

So as you can see a function definition in Python begins with the word “def” followed by the function name (in this case helloWorld), and finally parameters in parenthesis with a colon at the end of the line.

Now this is where things get a little different. Instead of using regular block characters, we simply only indent the code to signify the code is apart of that block. It does not necessarily matter how many spaces or tabs you use in your code, however you must be consistent with how many spaces you use.

Now as you can see this function simply returns the word Hello with the name appended to it (with a space in between).

So now you can use the function in your code:

print helloWorld("Python")

That’s it. You can how say Hello World in Python.

PHP Function – str_replace

The str_replace function is used to replace part of a string with another string. The syntax of this PHP function is detailed below:

<?
mixed str_replace( $search, $replace, $subject, $count );
?>

$search – This can either be a string value or an array of strings. This will be the string or strings you want to be replaced.

$replace – This can either be a string or an array of strings. These will be the value that is replaced when one of the $search items are found.

$subject – The string that will be searched and have replacements placed inside of it.

$count (optional) – The maximum number of times a replace should occur.

There are a number of reasons why you would want to use this function. One of the ways that I use str_replace is when I need to remove line breaks from the a large string that you are working with. You would do that by doing the following:

<?
$input = str_replace( "\n", '', $input );
?>

You may have noticed that I used $input as the return variable and the string that is having the replace performed on it. This is a quirk that I use in order to not have to create extra variables and you cannot pass the $input to the function as reference.

Ternary Operator – Your Best Friend

If you have never used the Ternary Operator, or ? operator, your life is about to change. The ternary operator is used as a shorthand for if .. else structures that can really clean up your code. Some may want to classify this under a intermediate or advanced skill, but it is so useful that I think it should be included with the most basic tutorials.

Take this for example:

<?
if( $use_live_server ) {
     $url = "http://www.thelivesite.com"
} else {
     $url = "http://www.thedevsite.com";
}
?>

This block of code sets which URL we want to connect to based on if a variable called $use_live_server. While the block of code is perfectly valid, there is a much better way to do the exact same thing in a single line of code.

<?
$url = $use_live_server ? "http://www.thelivesite.com" : "http://www.thedevsite.com";
?>

That one line of code does the exact same thing and once you understand how this works. Here is how the ternary operator works:

<?
$variable = $expression_to_evaulate ? true : false
?>

You do not necessarily have to have a variable that is true or false. You can do any expression you need to.