Site icon MiltonMarketing.com – Bernard Aybout's Blog

Python Basic Operators

Python Basic Operators : Just like any other programming languages, the addition, subtraction, multiplication, and division operators can be used with numbers.

[python] number = 1 + 2 * 3 / 4.0
print(number)[/python]

Trinket.io on-line Python compiler

Try to predict what the answer will be. Does python follow the order of operations? (of course, it does.)

Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder.

[python] remainder = 11 % 3
print(remainder)[/python]

Trinket.io on-line Python compiler

Using two multiplication symbols makes a power relationship. (power sign.) Exponentiation in python. So a**b means a^b.

[python] squared = 7 ** 2
cubed = 2 ** 3[/python]

Trinket.io on-line Python compiler

Using Operators with Strings

Python supports concatenating strings using the addition operator:

[python] helloworld = "hello" + " " + "world"
print(helloworld)[/python]

Trinket.io on-line Python compiler

Python also supports multiplying strings to form a string with a repeating sequence:

[python] lotsofhellos = "hello" * 10
print(lotsofhellos)[/python]

Trinket.io on-line Python compiler

Using Operators with Lists

Lists can be joined with the addition operators:

[python] even_numbers = [2,4,6,8] odd_numbers = [1,3,5,7] all_numbers = odd_numbers + even_numbers
print(all_numbers)[/python]

Trinket.io on-line Python compiler

Just as in strings, Python supports forming new lists with a repeating sequence using the multiplication operator:

[python] print([1,2,3] * 3)[/python]

Trinket.io on-line Python compiler

Exercise

The target of this exercise is to create two lists called x_list and y_list, which contain 10 instances of the variables x and y, respectively. You are also required to create a list called big_list, which contains the variables x and y, 10 times each, by concatenating the two lists you have created.

[python] x = object()
y = object()

# TODO: code already changed. this is the answer.
x_list = [x] * 10
y_list = [y] * 10
big_list = x_list + y_list

print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))

# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there…")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")[/python]

Trinket.io on-line Python compiler

 

Related Videos:

Related Posts:

My little pony learning game in VB.NET

Methods of teaching programming

Learn Python Lists

Learn about Python Conditions

Learn RE – Regular Expressions in Python

What is Python?

Learn List Comprehensions in Python Programming

Python String Formatting

Introduction to Python Programming Language

Learn about programming Functions in Python

Learn Partial functions Python programming

Learn Modules and Packages in Python programming

Object-Oriented Programming (OOP)

Learn about Dictionaries datatype for Python

How can Alice help teach OOP (Object Oriented Programming)?

Introduction to JavaScript – Variables: String Interpolation

Introduction to JavaScript

Learn about Numpy Arrays in Python programming

JavaScript Comparison Operators

Introduction to JavaScript – Variables: Review

Learn about programming Loops in Python

Introduction to JavaScript – CONSOLE

Introduction to JavaScript – Variables

Using the Current Year in Your Posts – WordPress

Learn Python Hello World Program

How to make a go-back button with PHP code?

Exception Handling in Python programming

Why do most sites use cookies?

Who is this Android App Development course for?

Hello World Android app built with Android Studio

Learn about Python Generators

Android Studios

Your first Hello World app in C# Sharp

Python Basic String Operations

Python Serialization (JSON)

FAQs

Exit mobile version