Python Basics Python Basics
Variables Math Operators Variables Math Operators
You can name a variable Operators Operation Example You can name a variable Operators Operation Example
anything as long as it anything as long as it
obeys the following rules: ** Exponent 2 ** 3 = 8 obeys the following rules: ** Exponent 2 ** 3 = 8
1. It can be only one % Modulus/Remainder 22 % 8 = 6 1. It can be only one % Modulus/Remainder 22 % 8 = 6
word. word.
2. It can use only let- 2. It can use only let-
ters, numbers, and the // Integer division 22 // 8 = 2
ters, numbers, and the // Integer division 22 // 8 = 2
underscore (_) character. underscore (_) character.
3. It can’t begin with a / Division 22 / 8 = 2.75 3. It can’t begin with a / Division 22 / 8 = 2.75
number. number.
4. Variable name starting 4. Variable name starting
with an underscore (_) are * Multiplication 3 * 3 = 9
with an underscore (_) are * Multiplication 3 * 3 = 9
considered as "unuseful`. considered as "unuseful`.
- Subtraction 5 - 2 = 3 - Subtraction 5 - 2 = 3
Example: Example:
spam = ’Hello’ + Addition 2 + 2 = 4 spam = ’Hello’ + Addition 2 + 2 = 4
_spam = ’Hello’ _spam = ’Hello’
Comments Comments
Inline comment: # This is a comment Inline comment: # This is a comment
Multiline comment: # This is a Multiline comment: # This is a
# multiline comment # multiline comment
Code with a comment: a = 1 # initialization Code with a comment: a = 1 # initialization
Please note the two spaces in front of the comment. Please note the two spaces in front of the comment.
Function docstring: def foo(): Function docstring: def foo():
""" This is a function docstring """ This is a function docstring
You can also use: You can also use:
''' Function Docstring ''' ''' Function Docstring '''
""" """
Data types Data types
Data Type Examples Data Type Examples
Integers -2, -1, 0, 1, 2, 3, 4, 5 Integers -2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25 Floating-point numbers -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25
Strings 'a', 'aa', 'aaa', 'Hello!', 'W0rld' Strings 'a', 'aa', 'aaa', 'Hello!', 'W0rld'
[Link] [Link]
Python Basics Python Basics
Conditions Loops Comparison Conditions Loops Comparison
One block: While: Equal to: One block: While: Equal to:
x = 3 x = 0 x == 3: x = 3 x = 0 x == 3:
if x == 3: while x < 4: if x == 3: while x < 4:
print(x) Not equal to: print(x) Not equal to:
print('x is 3') print('x is 3')
x = x + 1 x != 3: x = x + 1 x != 3:
Two blocks: Exiting a loop Two blocks: Exiting a loop
Less than: Less than:
mark = 80 using break: mark = 80 using break:
if mark >= 50: x < 3: if mark >= 50: x < 3:
print('pass') x = 0 print('pass') x = 0
while x < 4: Greater than: while x < 4: Greater than:
else: else:
print('fail') print(x) x > 3: print('fail') print(x) x > 3:
if x == 2: if x == 2:
break Less than or break Less than or
Multiple blocks: x = x + 1 Multiple blocks: x = x + 1
equal to: equal to:
mark = 80 mark = 80
if mark >= 65: Restart the loop x <= 3: if mark >= 65: Restart the loop x <= 3:
print('credit') using continue: print('credit') using continue:
elseif mark >= 50: Less than or elseif mark >= 50: Less than or
print('pass') x = 0 equal to: print('pass') x = 0 equal to:
else: while x <= 10: else: while x <= 10:
print('fail') x = x + 1 x >= 3: print('fail') x = x + 1 x >= 3:
if x % 2 == 0: if x % 2 == 0:
continue continue
print('%s is odd' % x) print('%s is odd' % x)
The result of The result of
Range For:
a comparison Range For:
a comparison
is a boolean: is a boolean:
Counts from 0 to 9: for i in range(10): Counts from 0 to 9: for i in range(10):
range(10) print(i) range(10) print(i)
True True
Starts from 0 and goes Iterates over string: Starts from 0 and goes Iterates over string:
or or
up to, not including 10 for c in ‘Hello’: up to, not including 10 for c in ‘Hello’:
print(c) False print(c) False
Counts from 1 to 10: Counts from 1 to 10:
range(1, 11) range(1, 11)
Input and output Input and output
Counts from 10 to 1: Counts from 10 to 1:
Print message: Print message:
range(10, 0, -1) range(10, 0, -1)
print('Hello world!') print('Hello world!')
Counts in steps of 2: Counts in steps of 2:
range(0, 11, 2) Print multiple values: range(0, 11, 2) Print multiple values:
Counts down ndays = 365 Counts down ndays = 365
print('There are', ndays, 'in a year') print('There are', ndays, 'in a year')
in steps of 2: in steps of 2:
range(10, 0, -2) Ask user for a string: range(10, 0, -2) Ask user for a string:
name= input('What is your name? ') name= input('What is your name? ')
Ask user for a whole number: Ask user for a whole number:
num = int(input('Enter a number: ')) num = int(input('Enter a number: '))
[Link] [Link]