Python: Difference between revisions
From Cheatsheet
Jump to navigationJump to search
(→for) |
|||
| Line 155: | Line 155: | ||
for char in name: | for char in name: | ||
print(char) | print(char) | ||
# For loop that checks for "top" inside words and lists the words | # For loop that checks for "top" inside words and lists the words | ||
| Line 166: | Line 161: | ||
print(word) | print(word) | ||
print('Done.') | print('Done.') | ||
</syntaxhighlight> | |||
=== Range === | |||
<syntaxhighlight lang="python"> | |||
# Print digits 0 through 3 using the range function | |||
for number in range(3): | |||
print(number) | |||
# Print digits 0 through 3 but don't output on a newline | |||
for number in range(3): | |||
print(number, end=" ") | |||
# Print digit for every 2 numbers between 0 and 11, starting from 0 | |||
for number in range(0, 11, 2): | |||
print(number, end=" ") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Template == | == Template == | ||
Revision as of 14:45, 7 February 2024
Applications & Webpages
- Python IDLE
- Pycharm
- Intellij Idea
Basic Syntax
# Leave a comment by prepending a "#" to your line
# Print "Hello World"
>>> print("Hello World!")
Hello World!
# Echo 3 strings
>>> "Hello" + " " + "World"
'Hello World'
# Add a newline to a value
>>> print("Hello World!\n")
Hello World!
# Escape an escape character
>>> print("Hello World\\n")
Hello World\n
# Single bracket are usable inside double-quotes
>>> "Hello 'orld"
"Hello 'orld"
# Escape a double bracket
>>> "Hello \"World"
'Hello "World'
Value types
# Find out the type of a value
>>> type (1 + 2)
<class 'int'>
>>> type (4 / 2)
<class 'float'>
>>> type("hello")
<class 'str'>
>>> type(False)
<class 'bool'>
# Set a value as a specific value-type
>>> int(4.0)
4
>>> float(2)
2.0
Basic math
# Add values together >>> 2 + 3 5 # Two to the power of 3 >>> 2 ** 3 8 # Divide whole numbers >>> 9 / 2 4.5 # Divide whole number but print the number on the left side of the comma >>> 9 // 2 4 # Modulo - The remainder after subtracting 2 as many times as you can >>> 9 % 2 1 # Display the lowest or highest value of a set values >>> min(5, 1, 8) 1 >>> max(5, 1, 8) 8
True and False
# True and False >>> True - False 1 >>> False False >>> False + False 0 >>> int(False) 0 >>> int(False) + int(True) 1 # Comparisons >>> 2 < 3 True >>> 2 > 3 False >>> 2 == 3 False >>> 2 != 3 True
Variables
# Define a variable >>> a = 3 # Display the contents of a variable >>> a 3 >>> s = "Apple" >>> s[0] 'A'
Common blocks
if
# If block and expression
if ( n % 2 == 1 ):
print("Weird")
# if and elif blocks with expressions
if ( n % 2 == 1 ):
print("Weird")
elif ( n >= 3 and n <= 5 and n % 2 == 0):
print("Not Weird")
for
# Basic for loop printing each character separately
for char in name:
print(char)
# For loop that checks for "top" inside words and lists the words
for word in ('stop', 'desktop', 'post', 'top'):
if 'top' in word:
print(word)
print('Done.')
Range
# Print digits 0 through 3 using the range function
for number in range(3):
print(number)
# Print digits 0 through 3 but don't output on a newline
for number in range(3):
print(number, end=" ")
# Print digit for every 2 numbers between 0 and 11, starting from 0
for number in range(0, 11, 2):
print(number, end=" ")