Python Basics 03-20-2014, 04:39 AM
#1
Table of Contents
Printing Messages
This is probably the simplest thing that you can do. When printing messages in the Python language, use the print function. Depending on your Python version, this function varies on how it is used.
- Printing with Version 3
- Printing with Versions below 3
Defining Variables
Defining variables in Python doesn't require and special syntax, a prime example of this would be in PHP, it uses the dollar symbol to define variables, but they must end with a semicolon.
However, in Python, you don't need to use any symbols, you just define what needs be.
Just like in other languages, you can used defined variables later in your code. We will talk more about this in the Message Formatting section.
Not only can you define messages, but as most languages, you can set booleans. We will talk more -- a lot more -- about booleans in the True or False Booleans section.
Message Formatting
When printing messages, or returning messages, you can use your defined variables to be shown in messages or other variables. If we want to add a variable, there's three ways you can go about this.
- Format
- Strings
- Percent Operator
Numbers
There's two types of numbers in the Python language, floats and integers. Floats are the numbers that have decimals, whilst intergers are full numbers with NO fecimal.
When defining a number, you can use it in true or false booleans, a string, and and a view other uses that won't be noted.
When doing the counting system for selecting letters, you use numbers. But it counts from zero instead of one.
Grayson
0123456
True and False Booleans
When using booleans, as mentioned before in Defining Variables, you can define a variable to be True or False, with that capitalization. The Python language will not read "true" or "false" and will parse an error, it can only be "True" or "False".
You can also do switches with booleans, it's simple. Below is an example of using booleans as switches.
We'll get more into boolean switching and such in the If and Else Statements section.
If and Else Statements
In the Python language, there's more than just if and else, there's elif, which is else if.
This is how it works;
You can use if/else statements in boolean switches, these are helpful because you can check if "condition" is true or false. If true/false, depending on the boolean you set, then the set condition will be triggered.
Commenting
When commenting your code in the Python langauge, you use hashtags. But only in certain places.
If your comment is before your condition, variable, whatever it may be, it will not run the set of code behind it. This is because of how commenting works, it tells the language not to read anything behind it, until the next line.
Hope this was useful.
~Dub
- Printing messages
- Defining Variables
- Message Formatting
- Numbers
- True and False Booleans
- If and Else Statements
- Commenting
Printing Messages
This is probably the simplest thing that you can do. When printing messages in the Python language, use the print function. Depending on your Python version, this function varies on how it is used.
- Printing with Version 3
Code:
print('Duubz')- Printing with Versions below 3
Code:
print 'Duubz'Defining Variables
Defining variables in Python doesn't require and special syntax, a prime example of this would be in PHP, it uses the dollar symbol to define variables, but they must end with a semicolon.
PHP Code:
$variable = 'Defined variable';
However, in Python, you don't need to use any symbols, you just define what needs be.
Code:
variable = 'Defined variable'Just like in other languages, you can used defined variables later in your code. We will talk more about this in the Message Formatting section.
Not only can you define messages, but as most languages, you can set booleans. We will talk more -- a lot more -- about booleans in the True or False Booleans section.
Message Formatting
When printing messages, or returning messages, you can use your defined variables to be shown in messages or other variables. If we want to add a variable, there's three ways you can go about this.
- Format
Code:
name = 'Duubz'
real = 'Grayson'
lang = 'Python'
print('{0} "{1}" is a {2} programmer.'.format(real,name,lang))- Strings
Code:
name = 'Duubz'
real = 'Grayson'
print(real+' '+name)- Percent Operator
Code:
name = 'Duubz'
print('Who? Oh, %s!' % (name))Numbers
There's two types of numbers in the Python language, floats and integers. Floats are the numbers that have decimals, whilst intergers are full numbers with NO fecimal.
When defining a number, you can use it in true or false booleans, a string, and and a view other uses that won't be noted.
Code:
number = 5.5
print(float(number))Code:
number = 5
print(int(number))When doing the counting system for selecting letters, you use numbers. But it counts from zero instead of one.
Grayson
0123456
Code:
select = 'Grayson'[4]
print(select)
# 4 => s
# Prints "s"True and False Booleans
When using booleans, as mentioned before in Defining Variables, you can define a variable to be True or False, with that capitalization. The Python language will not read "true" or "false" and will parse an error, it can only be "True" or "False".
Code:
duubz = TrueYou can also do switches with booleans, it's simple. Below is an example of using booleans as switches.
Code:
bool_t = False
bool_f = False
x = input('Number: ')
inte = int(x)
if inte > 5:
bool_t = True
else:
bool_f = True
if bool_t == True:
print('True')
else:
print('False')We'll get more into boolean switching and such in the If and Else Statements section.
If and Else Statements
In the Python language, there's more than just if and else, there's elif, which is else if.
This is how it works;
Code:
if condition:
do_condition1
elif condition1:
do_condition2
else:
do_condition3You can use if/else statements in boolean switches, these are helpful because you can check if "condition" is true or false. If true/false, depending on the boolean you set, then the set condition will be triggered.
Commenting
When commenting your code in the Python langauge, you use hashtags. But only in certain places.
Code:
hi = 'Hello!' #Comment!
print(hi)If your comment is before your condition, variable, whatever it may be, it will not run the set of code behind it. This is because of how commenting works, it tells the language not to read anything behind it, until the next line.
Code:
#This is NOT okay! hi = 'Hello!'
print(hi)Code:
hi = 'Hello!' #This IS okay!
print(hi)Hope this was useful.
~Dub
![[Image: BXqGARG.png]](https://i.imgur.com/BXqGARG.png)










![[+]](https://sinister.li/images/modern/collapse_collapsed.png)