Python Functions

Python Functions

Python Functions


A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.


Creating a Function

In Python a function is defined using the def
keyword:

Example

def my_function():
  print("Hello from a function")


Calling a Function

To call a function, use the function name followed by parenthesis:

Example

def my_function():
  print("Hello from a function")

my_function()

Run example »


Parameters

Information can be passed to functions as parameter.

Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a comma.

The following example has a function with one parameter (fname).
When the function is called, we pass along a first name,
which is used inside the function to print the full name:

Example

def my_function(fname):
  print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")

Run example »



Default Parameter Value

The following example shows how to use a default parameter value.

If we call the function without parameter, it uses the default value:

Example

def my_function(country = "Norway"):
  print("I am from " +
country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")

Run example »


Return Values

To let a function return a value, use the return
statement:

Example

def my_function(x):
  return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))

Run example »


Recursion

Python also accepts function recursion, which means a defined function can call itself.

Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming.

In this example, tri_recursion() is a function that we have defined to call itself ("recurse"). We use the k variable as the data, which decrements (-1) every time we recurse. The recursion ends when the condition is not greater than 0 (i.e. when it is 0).

To a new developer it can take some time to work out how exactly this works, best way to find out is by testing and modifying it.

Example

Recursion Example

def tri_recursion(k):
 
if(k>0):
   
result = k+tri_recursion(k-1)
   
print(result)
 
else:
   
result = 0
 
return result

print("\n\nRecursion Example Results")
tri_recursion(6)

Run example »



via W3Schools Online Web Tutorials

March 31, 2019 at 08:50PM

Python String lower() – Python Standard Library

Python String lower() – Python Standard Library

Python String lower()

The string lower() method converts all uppercase characters in a string into lowercase characters and returns it.

The syntax of lower() method is:

 string.lower()

String lower() Parameters()

The lower() method doesn’t take any parameters.


Return value from String lower()

The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase.

If no uppercase characters exist, it returns the original string.


Example 1: Convert a string to lowercase

 # example string string = "THIS SHOULD BE LOWERCASE!" print(string.lower()) # string with numbers # all alphabets whould be lowercase string = "Th!s Sh0uLd B3 L0w3rCas3!" print(string.lower()) 

When you run the program, the output will be:

 this should be lowercase! th!s sh0uld b3 l0w3rcas3! 

Example 2: How lower() is used in a program?

 # first string firstString = "PYTHON IS AWESOME!" # second string secondString = "PyThOn Is AwEsOmE!" if(firstString.lower() == secondString.lower()): print("The strings are same.") else: print("The strings are not same.") 

When you run the program, the output will be:

 The strings are same. 

Note: If you want to convert to uppercase string, use upper(). You can also use swapcase() to swap between lowercase to uppercase.

via Learn Programming: Tutorials and Examples from Programiz

March 31, 2019 at 08:50PM

Web Development

Web Development

The Web has grown a mindboggling amount in the number of sites, users and implementation capabilities since the first website went live in 1989. Web development is the concept that encompasses all the activities involved with websites and web applications.

Python is not used in a web browser. The language executed in browsers such as Chrome, Firefox and Internet Explorer is JavaScript. Projects such as pyjs can compile from Python to JavaScript. However, most Python developers write their web applications using a combination of Python and JavaScript. Python is executed on the server side while JavaScript is downloaded to the client and run by the web browser.

To become an experienced web developer you need to know the foundation principles that the web is built with, such as HTTP requests and responses, client (typically web browsers) and server (web servers such as Nginx and Apache architectures, HTML, CSS and JavaScript, among many other topics. The following resources provide a range of perspectives and when combined together should get you oriented in the web development world.

  • How the Internet works is a must-read to get a quick overview of all the pieces that go into a network connection from one machine to another. The example explains how an email is sent and the story is just as useful for learning about other connections such as downloading a webpage.

  • If you want to be a web developer it’s important to know the foundational tools used to build websites and web applications. It is also important to understand that the core concepts such as HTTP, URLs and HTML were all there at the beginning and then were expanded with new specifications over time. This article on the History of the Web succinctly explains the origins of the web starting from Tim Berners-Lee’s origin vision and release at CERN.

  • Web Architecture 101 is a great high-level overview of the technologies that run the modern web, such as DNS, load balancers, web application servers (for Python that equates to WSGI servers), data bases, task queues, caching and several other critical concepts.

  • The Evolution of the Web visualizes how web browsers and related technologies have changed over time as well as the overall growth of the Internet in the amount of data transferred. Note that the visualization unfortunately stops around the beginning of 2013 but it’s a good way to explore what happened in the first 24 years.

  • How browsers work provides an overview with solid detail on how browsers take the HTML, CSS, JavaScript, images and other files as input and render webpages as output. It is well worth your time to know this stuff as a web developer.

  • Web app checklist presents good practices that developers building and deploying web applications should follow. Don’t worry about having every single one of these recommendations implemented before getting your site live, but it is worthwhile to review the list to make sure there is not something obvious you can handle in a few minutes that will improve your site’s security, performance or usability.

  • Web application development is different and better provides some context for how web development has evolved from writing static HTML files into the complex JavaScript client-side applications produced today.

  • The Browser Hacker’s Guide to Instantly Loading Everything is a spectacular technical talk given by Addy Osmani at JSConf EU 2017 that has great bits of developer knowledge for both beginner and experienced web developers alike.

  • Build a web application from scratch and its follow on posts for request handling middleware explores the fundamentals of web development. Learning these foundational concepts is critical for a web developer even though you should still plan to use an established web framework such as Django or Flask to build real-world applications. The open source code for these posts is available on GitHub.

  • While not Python-specific, Mozilla put together a Learning the Web tutorial for beginners and intermediate web users who want to build websites. It’s worth a look for general web development learning.

  • Web development involves HTTP communication between the server, hosting a website or web application, and the client, a web browser. Knowing how web browsers works is important as a developer, so take a look at this article on what’s in a web browser.

  • Ping at the speed of light dives into the computer networking weeds with how fast packets travel through the internet plumbing. The author created a Python script that scrapes network speeds from disparate locations to see what the network speed is in fiber optic cables as a percentage of the speed of light.

  • The critical path: optimizing load times with the Chrome DevTools provides a well-written explanation about using Chrome’s developer features to improve the performance of your websites and web applications.

  • Three takeaways for web developers after two weeks of painfully slow Internet is a must-read for every web developer. Not everyone has fast Internet service, whether because they are in a remote part of the world or they’re just in a subway tunnel. Optimizing sites so they work in those situations is important for keeping your users happy.

  • The History of the URL: Path, Fragment, Query, and Auth gives a comprenhensive historical perspective on the fundamental way to link to resources on the web. This post should be required reading for web developers.

  • Quantum Up Close: What is a browser engine? explains how a browser takes in HTML, JavaScript, CSS, images and any other data and files to produce a webpage as output.

  • How to understand performance tests is an important topic because many websites are slow and bloated. Learning about improving the performance of your site is one of the best ways to become a better web developer. Another great article on website performance is The average web page is 3MB. How much should we care?. The visuals alone tell a compelling story about how large webpage sizes have grown in recent years.

via

March 31, 2019 at 08:49PM

Basic Code Examples – Python Codes

Basic Code Examples – Python Codes

Printing:

Print is a function that will print whatever you want onto the screen. For example:

print(“Hello everyone”)

This prints Hello everyone to the screen.

Input:

Input is a function which is used to ask the user something or get something from the user. For example, if i wanted to know someone’s name:

name = input(“what is your name: “)

You can make name anything you want. It doesn’t have to be called name, although it is easiest to name the variable something that is related to it. The variable is whatever you are making the input equal. In this case name is the variable.

Integer Inputs:

Say I wanted to ask for someone’s age, I would write:

age = int(input(“what is your age: “))

We write int because age is a number, therefore is an integer.

Printing Inputs:

If I wanted to print the name:

name = input(“what is your name: “)
print(name)

This prints to the screen whatever the user entered as their name. To print an integer, like age:

age = int(input(“how old are you? “))
print(age)

Numeracy:

We can use python to add, multiply, divide, subtract integers, but first we need to get the numbers:

num1 = int(input(“enter a number: “))
num2 = int(input(“enter a number: “))

Now we can add them together:

totalSum = (num1) + (num2)
print(totalSum)

This is the same with subtraction, division and multiplication, we just simply swap the + with – (subtraction), / (division), * (multiplication).

Printing Multiple Inputs in one Line:

If we wanted to print multiple inputs in a sentence, we would use this:

name = input(“what is your name: “)
age = int(input(“how old are you? “))
print(“hello ” + (name) + “, you are ” + str(age) + ” years old!”)

via Python Codes

March 30, 2019 at 08:13AM

Atlassian, Trello, Kanban and WordPress – Kanban for WordPress

Atlassian, Trello, Kanban and WordPress – Kanban for WordPress

It was announced earlier this week that Atlassian has acquired Trello.

Atlassian is a software company that has a product called Jira, launched in 2002. Jira is extremely powerful but is very focused on agile (Scrum, Lean, etc) project management for businesses. Part of their solution is a Kanban board view, which they added in the last few years.

Trello, built by Fog Creek (run by Joel Spolsky, the man behind products like Stack Overflow), was introduced as a team collaboration tool, with a few mentions of project management, in 2011. Trello is a Kanban board, but you’ll never hear them use the word Kanban.

In the past year, GitHub has added a Kanban board view of issues, and Teamwork is slated to add Kanban “soon”.

And we launched Kanban for WordPress a little more than a year ago.

Kanban is experiencing a spike in popularity, and I would attribute it entirely to Trello.

Trello has done a great job of presenting Kanban as a simple pattern, a swiss army knife, the new to-do list, and useful for most anything. They’ve got tutorials for organizing your family, tracking a home remodeling, managing job applicants, planning a vacation, tracking ideas and more.

I’ll admit when I first launched Kanban for WordPress, I only thought of the Kanban method as a project management solution. Then I saw pipedrive.com use it as a CRM, workable.com use it for tracking job applicants, and of course Trello.

The interesting parallel has been the growth of WordPress beyond a content management system (CMS). More people are building apps on WordPress every day, and more plugins are empowering WordPress site to do more internally: intranets, CRM’s, file sharing, project management, and more.

I won’t speculate on why Atlassian bought Trello (though it is fun!), but I do look forward to seeing how tools like WordPress and Kanban, once thought of as single purpose tools, make our work, communication, and lives better.

via Kanban for WordPress

March 27, 2019 at 09:52AM

WordPress 5.2 Beta 1

WordPress 5.2 Beta 1 is now available!

This software is still in development, so we don’t recommend you run it on a production site. Consider setting up a test site to play with the new version.

You can test the WordPress 5.2 Beta two ways:

WordPress 5.2 is slated for release on April 30, and we need your help to get there. Here are some of the big items to test so we can find as many bugs as possible in the coming weeks.

Block Editor

The block editor has received significant performance improvements since WordPress 5.1, shaving 35% off the load time for massive posts, and cutting the keypress time (how responsive it feels when you’re typing) in half!

Accessibility continues to improve, too. The block editor now supports your browser’s reduced motion settings. The post URL slug has better labelling and help text. The focus styling for keyboard navigating through landmarks is clearer and more consistent. There are a variety of new speak messages, and existing messages have been tweaked for more useful screen-reader behaviour.

We’ve added several new blocks:

  • An RSS block
  • An Amazon Kindle embed block
  • A Search block
  • A Calendar block
  • A Tag Cloud block

To help you keep track of these blocks, and only show the ones you need, there’s a new block management tool to switch blocks on and off.

Block Management Modal

We’re constantly working on existing blocks, too. There are hundreds of bug fixes and improvements in the block editor, you can read more about them in the Gutenberg plugin releases: 4.9, 5.0, 5.1, 5.2, and 5.3.

The WordPress Mobile Apps

The block editor isn’t just for websites, either. The WordPress mobile apps now include an experimental version of a built-in block editor. This is still under development, but you can try it out now!

Site Health Check

Site Health Check is an ongoing project aimed at improving the stability and performance of the entire WordPress ecosystem.

The first phase of this project (originally scoped for WordPress 5.1) is now included in WordPress 5.2. For the first time, WordPress will catch and pause the problem code, so you can log in to your Dashboard and see what the problem is (#44458). Before, you’d have to FTP in to your files or get in touch with your host.

In addition, we’re adding a new Health Check tool to your Dashboard. Visit the Tools menu and click on Health Check to get information that can help improve the speed and security of your site.

PHP Version Bump

With this release, WordPress will increase its minimum supported PHP version to 5.6. To help you check if you’re prepared for this change, WordPress 5.2 will show you a warning and help you upgrade your version of PHP, if necessary.

For Developers

  • Plugins can now specify the minimum version of PHP that they support, so you can safely modernise your development practices without risking breaking your users’ sites. (#40934)
  • We’ve added the sodium_compat library, which provides backwards compatibility for the Sodium-based cryptography library added in PHP 7.2. (#45806)
  • There’s a new release of Dashicons, the WordPress Dashboard icon font. There are 25 new icons for you to use! (#41074)
  • You can now pass a label to get_search_form(), improving accessibility. (#42057)

There have been 130 tickets closed in WordPress 5.2 so far, with numerous small bug fixes and improvements to help smooth your WordPress experience.

Keep your eyes on the Make WordPress Core blog for developer notes (which are assigned the dev-notes tag) in the coming weeks detailing other changes in 5.2 that you should be aware of.

How to Help

Do you speak a language other than English? Help us translate WordPress into more than 100 languages!

If you think you’ve found a bug, you can post to the Alpha/Beta area in the support forums. We’d love to hear from you! If you’re comfortable writing a reproducible bug report, file one on WordPress Trac, where you can also find a list of known bugs.


With each new release,
bearing multiple betas;
We fix, then we fly.

Source

5 Uses of Absolute Positioning

5 Uses of Absolute Positioning

If you’re an experienced web designer, you may wonder “How does it work for responsive design?” It’s a well-known fact that absolutely positioning breaks a responsive design — if you use absolute positioning on an item and don’t adjust it for various screen sizes, you’ll end up with a situation where your item goes off the canvas. A wrongly positioned object can either not display on some screens (usually, on mobile), or it can increase the total canvas area (meaning the page will overflow the viewport, and the visitor will need to scroll through tons of whitespace).

The Elementor team is familiar with this problem and solves it by allowing designers to set a separate custom position for mobile, tablet, and desktop devices. It’s possible to use percentage, VH, VW or pixel units to position your element. They also added an Overflow Hidden control, which condenses all of the section widgets inside it and removes any unwanted scrolls.

Now let’s see how you can use absolute positioning to create a unique design:

WordPress

via Elementor Blog

March 27, 2019 at 03:53AM

Automate the Boring Stuff with Python: Practical Programming for Total Beginners: Al Sweigart: 9781593275990: Amazon.com: Books

Automate the Boring Stuff with Python: Practical Programming for Total Beginners: Al Sweigart: 9781593275990: Amazon.com: Books

"I’m having a lot of fun breaking things and then putting them back together, and just remembering the joy of turning a set of instructions into something useful and fun, like I did when I was a kid." —Wil Wheaton

"Do you need Automate the Boring Stuff with Python? Yes, if you want to enhance your workflow by using automation, this is an excellent place to start. Highly recommended."—Network World

via Robot Check

March 26, 2019 at 08:22PM

Python Insider: Python 3.8.0a3 is now available for testing

Python Insider: Python 3.8.0a3 is now available for testing

Go get it here:
https://www.python.org/downloads/release/python-380a3/

The most visible change so far is probably the implementation of PEP 572: Assignment Expressions.  For a detailed list of changes, see:
https://docs.python.org/3.8/whatsnew/changelog.html

Python 3.8.0a3 is the third of four planned alpha releases of Python 3.8, the next feature release of Python.  During the alpha phase, Python 3.8
remains under heavy development: additional features will be added and existing features may be modified or deleted.  Please keep in mind that this is a preview release and its use is not recommended for production environments.  The last alpha release, Python 3.8.0a4, is planned for 2019-04-29.

Thanks
to all of the many volunteers who help make Python development and these releases possible!  Please consider supporting our efforts by volunteering yourself or through organization contributions to the Python Software Foundation.

Python

via Planet Python

March 26, 2019 at 03:48AM