0% found this document useful (0 votes)
14 views1 page

Python Cheat Sheet Clean

Uploaded by

zeyadmonabil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Python Cheat Sheet Clean

Uploaded by

zeyadmonabil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Variables & Strings

Variables are used to store values.


A string is a series of characters, surrounded by single or double quotes.
Example:
print("Hello world!")

With a variable:
msg = "Hello world!"
print(msg)

Concatenation:
first_name = "albert"
last_name = "einstein"
full_name = first_name + " " + last_name
print(full_name)

Lists
Lists store a series of items in a particular order.
Example:
bikes = ['trek', 'redline', 'giant']
first_bike = bikes[0]
last_bike = bikes[-1]

Looping:
for bike in bikes:
print(bike)

Adding:
bikes.append('trek')

List comprehensions:
squares = [x**2 for x in range(1, 11)]

Slicing:
finishers = ['sam', 'bob', 'ada', 'bea']
first_two = finishers[:2]

Copying:
copy_of_bikes = bikes[:]

Tuples
Tuples are like lists but immutable.
Example:
dimensions = (1920, 1080)

If Statements
Conditional tests: ==, !=, >, >=, <, <=
Example:
if age >= 18:
print('You can vote!')

If-elif-else:
if age < 4:
ticket_price = 0
elif age < 18:
ticket_price = 10
else:
ticket_price = 15

You might also like