Category Python

center(): centered string in Python

how to python

Use the center() method to center a string. word = 'beach' number_spaces = 32 word_centered = word.center(number_spaces) print(word) #'beach' print(word_centered) ##output: ' beach ' Notice the spaces in the second string. The word ‘beach’ has 5 characters, which gives us…

Bitwise Operators in Python

how to python

Bitwise Operators allow you to perform operations on binary numbers. The values are automatically converted to binary and then the logic is applied to them. The output is also converted back from binary implicitly. They are: &: AND Only the…

How to find the largest number in Python

how to python

In this post, we will learn how to find the largest number. The first program uses the if statement to achieve that. The second program, the recommended way, uses the built-in max function and a list. Code using the if…

How to find the smallest number in Python

how to python

In this post, we will learn how to find the smallest number. The first program uses the if statement to achieve that. The second program, the recommended way, uses the built-in min function and a list. Code using the if…

How to add two numbers in Python

how to python

Let’s see how two perform a very common and simple task when learning a programming language: adding two numbers. In python the code for adding two numbers is very straightfoward. Adding numbers directly first_number = 32 second_number = 8 total…

Scope in Python

how to python

There are two scopes: local and global. Global Scope A global scope allows you to use the variable anywhere in your program. If your variable is outside a function, it has a global scope by default. name = "Bob" def…