Basics
If you're on this page, you most likely have not used Python much (or even at all) before. Good news: this website is designed for beginners.
If you're on this page, you most likely have not used Python much (or even at all) before. Good news: this website is designed for beginners.
Python has many different data types. One of these is a floating-point number.
A floating-point number is any real number, whether it is negative or has decimals. Python calls floating-point numbers float types. Any number you type (4, 3.5, -8) is automatically converted to a float.
Another type of number is an integer. Python calls this an int data type. An int is a positive or negative number WITHOUT decimal points.
To convert something to a float, use the float(object) function. It accepts all different data types. To convert something to an integer, use the int(object[,base]) function. The brackets around base mean that it's optional. Normally, we use base 10.
While we're on the subject of converting data types, let's learn how to call a function.
The basic format for calling a function is name(argument1,argument2,...).
Let's say we want to convert 43.5 to an integer. We'll use the int, which takes an object, and a base. The base that you want to use most of the time is 10. Our function call is int(43.5,10).
Let's say we want to calculate 56 times 74. That's hard, right? With Python, it's easy! To multiply two numbers, we use the * operator. An operator is like a function, only instead of using parentheses, we just write it in our program.
Anyway, to do that multiplication problem with Python, we just type 56 * 74 or 56*74. The number of spaces between the numbers and operator doesn't matter to Python.
More operators:
+ is used for addition. Ex: 2 + 3.
- is used for subtraction. Ex: 3 - 2.
* is used for multiplication. Ex: 2 * 3.
/ is used for division. Ex: 4 / 2.
% is used for mod. This is the same thing as the remainder in division. Ex: 3 % 2.
Strings are basically Python's form of text. You can surround a string with either single quotes ' or double quotes ". You can then write in the string. Ex: 'Some text', "Some more text", and 'Even more text'.
You can put quotes in your string, as long as they don't match the quotes on the outside. Example: 'This string has a " in it.'.
Another type of string is called a multiline string. You surround these with triple single quotes '''. The main difference between a multiline and regular string is that you can break a multiline string up over multiple lines. Example:
'''This string is broken up
over several lines.'''
"This string will produce
an error."
Don't break up a regular string over multiple lines, or Python produces an error.
Remember the + operator? If you use it with two strings, it will concatenate the two strings. Example: "Hello " + "world!" will produce "Hello world!".
Last thing about strings: Python calls string a str type. To convert something to a string, use the str() function. The float() function works fine with strings. However, if you try to call the int() function on a string with a decimal, it produces an error. To be fail-safe, use int(float(str)) to convert a string to an integer.
One of Python's output features is the console. The console is where programs are run. We're going to learn a way to add text (print text) to the console.
The way to do this is with the print(expr or str) function. To print text, run the print function with what you want to display as the parameter. Ex: print(2) prints 2 to the console. print("abc") prints abc to the console.
A program isn't much unless we can get input from the user. The way to do that is with the input([expr or str]) function. We can feed it an expression or question to display before the input.
Let's say we want to store a user's input. We can do this with a variable. To declare a variable, just write variable_name = value. A variable is basically a label. It just helps us avoid typing the same thing over and over. If we want to use a variable, we just write it. Ex:
# Here we set a variable to a user's input.
userInput = int(float(input("Enter a number:"))
# We then add two to it and print two result.
print("Your number plus two is " + str(userInput+2) + ".")
If we want to delete a variable, we use the del keyword. Ex:
userInput = int(float(input("Enter a number:"))
print("Your number plus two is " + str(userInput+2) + ".")
del userInput # We can delete userInput because we don't need it anymore.
Did you notice how in the previous example, we had to use the + operation a lot? There is a better way to do this, called embedding values. The way to do this is with a slightly altered version of the print() function, which is print(str_with_%s %(values)). In our previous example, we could have done print("Your number plus two is %s." %(userInput+2)). Python automatically replaces the %s with the value we gave it.
Create a program that asks the user for their name, then prints "Hello, (name)!"
Create a program that asks the user to input a number, adds it to 1, then prints "(number) + 1 = (sum)".
Floating-point number: Any number not in scientific notation.
Base: An integer greater than 2 that defines how operations work. For example, 1 + 1 = 10 in base 2.
Mod: An operation that returns the remainder of a division.
Error: An error is what occurs when your code is incorrect.
Expr: Short for expression. In Python, an expr is anything that is not a string.
%s: Used as a placeholder for strings in Python.
raw_input() Function
Escaping Characters
Remake challenge program 1, but use raw_input() instead of input().
Create a program that prints a string. The string MUST contain a quote that is the same as the outer quotes.